Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
| Author |
Topic |
|
thatzlim
Starting Member
6 Posts |
Posted - 2007-10-22 : 02:47:28
|
Hi, I have created a new Stored Procedure (SP):set ANSI_NULLS ONset QUOTED_IDENTIFIER ONGO-- =============================================-- Author: <Author,,Name>-- Create date: <Create Date,,>-- Description: <Description,,>-- =============================================ALTER PROCEDURE [dbo].[SPs_GetEventFilteredSorted] -- Add the parameters for the stored procedure here @BUID int = null, @Name nvarchar(MAX) = null, @DateTimeFrom datetime = null, @DateTimeFrom2 datetime = null, @Reporter nvarchar(MAX) = null, @Crew nvarchar(MAX) = null, @sql nvarchar(MAX), @sortExpression nvarchar(MAX)ASBEGIN -- SET NOCOUNT ON added to prevent extra result sets from -- interfering with SELECT statements. SET NOCOUNT ON; IF @BUID = 0 SET @BUID = null IF LEN(@Name) = 0 SET @Name = null ELSE SET @Name = '%' + @Name + '%' IF LEN(@Reporter) = 0 SET @Reporter = null ELSE SET @Reporter = '%' + @Reporter + '%' IF LEN(@Crew) = 0 SET @Crew = null ELSE SET @Crew = '%' + @Crew + '%' -- Insert statements for procedure here SET @sql = 'SELECT EventID, Name, Description, BUID, Location, DateTimeFrom, DateTimeTo, Embargo, Reporter, Crew, RSVP, GuestOfHonour, Remarks, Status, CreatedDate, CreatedBy, UpdatedDate, UpdatedBy FROM Event WHERE (BUID LIKE COALESCE(@BUID,BUID)) AND (Name LIKE COALESCE(@Name,Name)) AND (DateTimeFrom >= COALESCE(@DateTimeFrom,DateTimeFrom)) AND (DateTimeFrom <= COALESCE(@DateTimeFrom2,DateTimeFrom)) AND (Reporter LIKE COALESCE(@Reporter,Reporter)) AND (Crew LIKE COALESCE(@Crew,Crew)) ORDER BY ' + @sortExpression -- Execute the SQL query EXEC sp_executesql @sqlEND When I tried to execute it (Right-click the SP from Summary Page and select 'Execute Stored Procedure...')USE [CAS]GOEXEC [dbo].[SPs_GetEventFilteredSorted] @BUID = 0, @Name = N'Event', @DateTimeFrom = N'2007-10-17', @DateTimeFrom2 = N'2007-10-18', @Reporter = N'Reporter', @Crew = N'Crew', @sql = NULL, @sortExpression = N'EventID'GO I get this error:Msg 137, Level 15, State 2, Line 7Must declare the scalar variable "@BUID".How can I solved this problem? Please advice in details. Thank you!!! |
|
|
jackv
Master Smack Fu Yak Hacker
2179 Posts |
Posted - 2007-10-22 : 03:09:46
|
| The way to execute it as : USE [CAS]GOEXEC [dbo].[SPs_GetEventFilteredSorted] 0, 'Event', '2007-10-17', '2007-10-18', 'Reporter', 'Crew', NULL, 'EventID'GOJack Vamvas--------------------Search IT jobs from multiple sources- http://www.ITjobfeed.com |
 |
|
|
thatzlim
Starting Member
6 Posts |
Posted - 2007-10-22 : 03:19:26
|
quote: Originally posted by jackv The way to execute it as : USE [CAS]GOEXEC [dbo].[SPs_GetEventFilteredSorted] 0, 'Event', '2007-10-17', '2007-10-18', 'Reporter', 'Crew', NULL, 'EventID'GO
FYI, this script (as below) is generated by SQL Server when I tried to execute the SP.USE [CAS]GOEXEC [dbo].[SPs_GetEventFilteredSorted] @BUID = 0, @Name = N'Event', @DateTimeFrom = N'2007-10-17', @DateTimeFrom2 = N'2007-10-18', @Reporter = N'Reporter', @Crew = N'Crew', @sql = NULL, @sortExpression = N'EventID'GO However, I tried using the way you suggestedUSE [CAS]GOEXEC [dbo].[SPs_GetEventFilteredSorted]0,'Event','2007-10-17','2007-10-18','Reporter','Crew',NULL,'EventID'GO It returned the same error:Msg 137, Level 15, State 2, Line 7Must declare the scalar variable "@BUID". |
 |
|
|
rksingh024
Yak Posting Veteran
56 Posts |
Posted - 2007-10-22 : 03:28:14
|
| Since you are using constructed SQL string and having variable inside it, you are getting this error. To resolve error, construct correct sql string like(BUID LIKE COALESCE(' + convert(varchar,@BUID) + ',BUID)) ANDand similarly for others |
 |
|
|
Kristen
Test
22859 Posts |
Posted - 2007-10-22 : 03:45:17
|
| I don't think you need to use Dynamic SQL for this at allKristen |
 |
|
|
Kristen
Test
22859 Posts |
Posted - 2007-10-22 : 03:46:26
|
Also note that your error message:Msg 137, Level 15, State 2, Line 7Must declare the scalar variable "@BUID". Is NOT the Line for the Sproc, but the line for the @sql as sent to sp_ExecuteSQLKristen |
 |
|
|
thatzlim
Starting Member
6 Posts |
Posted - 2007-10-22 : 03:49:27
|
quote: Originally posted by Kristen I don't think you need to use Dynamic SQL for this at allKristen
I was told that to do a Sort, we must use Dynamic SQL. What would you suggest? |
 |
|
|
madhivanan
Premature Yak Congratulator
22864 Posts |
Posted - 2007-10-22 : 04:08:56
|
Instead of dynamic sql, you can do conditional sortingOrder by case when @var='somevalue then col1 else col2 endEDIT : Fixed typo MadhivananFailing to plan is Planning to fail |
 |
|
|
Kristen
Test
22859 Posts |
Posted - 2007-10-22 : 04:15:24
|
As Madhi says, but for a slightly fuller example:ORDER BY CASE WHEN @sortExpression = 'EventID' THEN EventID END, CASE WHEN @sortExpression = 'Col2' THEN Col2 END DESC, ... Kristen |
 |
|
|
Kristen
Test
22859 Posts |
Posted - 2007-10-22 : 04:39:02
|
"EDIT : Fixed typo"You going to fix it properly then?   |
 |
|
|
thatzlim
Starting Member
6 Posts |
Posted - 2007-10-22 : 05:29:18
|
quote: Originally posted by madhivanan Instead of dynamic sql, you can do conditional sortingOrder by case when @var='somevalue then col1 else col2 endEDIT : Fixed typo MadhivananFailing to plan is Planning to fail
it is not going to be 2 columns of sorting. that's why i'm using dynamic sql. |
 |
|
|
madhivanan
Premature Yak Congratulator
22864 Posts |
Posted - 2007-10-22 : 06:22:56
|
quote: Originally posted by Kristen "EDIT : Fixed typo"You going to fix it properly then?   
Yes I fixed improper words properly MadhivananFailing to plan is Planning to fail |
 |
|
|
madhivanan
Premature Yak Congratulator
22864 Posts |
Posted - 2007-10-22 : 06:25:56
|
quote: Originally posted by thatzlim
quote: Originally posted by madhivanan Instead of dynamic sql, you can do conditional sortingOrder by case when @var='somevalue then col1 else col2 endEDIT : Fixed typo MadhivananFailing to plan is Planning to fail
it is not going to be 2 columns of sorting. that's why i'm using dynamic sql.
Read Kristen's exampleMadhivananFailing to plan is Planning to fail |
 |
|
|
Kristen
Test
22859 Posts |
Posted - 2007-10-22 : 10:53:37
|
| "it is not going to be 2 columns of sorting. that's why i'm using dynamic sql."My example was for N columns ... |
 |
|
|
|
|
|
|
|