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.

 All Forums
 SQL Server 2005 Forums
 Transact-SQL (2005)
 Error in Dynamic SQL

Author  Topic 

sqlpal2009
Yak Posting Veteran

58 Posts

Posted - 2009-09-18 : 12:27:54
Hello All,

I am getting an error in the code below. I can print the SQL but I think I get an error when executing that final @SQL1. It doe snot like the @AdminUser, @UserCusts and UserProts variables.

Here is the error
Msg 137, Level 15, State 2, Line 30
Must declare the scalar variable "@AdminUser".


BEGIN

DECLARE @SQL1 NVARCHAR(MAX)
DECLARE @SQL_SELECT NVARCHAR(MAX)
DECLARE @SQL_WHERE NVARCHAR(MAX)
DECLARE @SQL_PAGING NVARCHAR(MAX)
DECLARE @Status INT
DECLARE @OrderBy VARCHAR(255)

DECLARE @AdminUser INT
DECLARE @UserCusts INT
DECLARE @UserProts INT

SELECT @AdminUser = CAST(AllCusts AS INT)
FROM UserSites
WHERE UserSites.UserID = @WebUserID
AND UserSites.ApplicationID = @ApplicationID
AND UserSites.SiteID = @SiteID
AND UserSites.AllCusts = 1

IF(SELECT COUNT(*) FROM OrderHistory
INNER JOIN UserCusts
ON OrderHistory.SiteID = UserCusts.SiteID
AND OrderHistory.CustNo = UserCusts.CustomerID
INNER JOIN UserSites
ON UserSites.UserID = UserCusts.UserID
AND UserSites.ApplicationID = UserCusts.ApplicationID
AND UserSites.SiteID = UserCusts.SiteID
WHERE OrderHistory.Protocol = @Protocol
AND UserCusts.ApplicationID = @ApplicationID
AND UserCusts.SiteID = @SiteID
AND UserCusts.UserID = @WebUserID
AND UserSites.AllCusts = 0
AND UserCusts.AllProts = 1 ) > 0
BEGIN
SELECT @UserCusts = 1
END
ELSE
BEGIN
SELECT @UserCusts = 0
END

IF(SELECT COUNT(*) FROM OrderHistory
INNER JOIN UserProts
ON OrderHistory.SiteID = UserProts.SiteID
AND OrderHistory.CustNo = UserProts.CustomerID
AND OrderHistory.Protocol = UserProts.Protocol
INNER JOIN UserCusts
ON UserCusts.UserID = UserProts.UserID
AND UserCusts.ApplicationID = UserProts.ApplicationID
AND UserCusts.SiteID = UserProts.SiteID
AND UserCusts.CustomerID = UserProts.CustomerID
INNER JOIN UserSites
ON UserSites.UserID = UserCusts.UserID
AND UserSites.ApplicationID = UserCusts.ApplicationID
AND UserSites.SiteID = UserCusts.SiteID
WHERE OrderHistory.Protocol = @Protocol
AND UserProts.ApplicationID = @ApplicationID
AND UserProts.SiteID = @SiteID
AND UserProts.UserID = @WebUserID
AND UserSites.AllCusts = 0
AND UserCusts.AllProts = 0) > 0
BEGIN
SELECT @UserProts = 1
END
ELSE
BEGIN
SELECT @UserProts = 0
END

select @AdminUser, @UserCusts, @UserProts

SET @OrderBy = 'CreatedByDate DESC, OH.Protocol'

SET @SQL_SELECT = '
SELECT ROW_NUMBER() OVER(ORDER BY ' + @OrderBy + ') AS RowNumber
,OrderHistoryID
,ST.StatusID
,LEFT(ST.StatusDescription
,CHARINDEX('' '', ST.StatusDescription) - 1) AS Status
,OH.StudyID AS StudyID
,OH.Protocol AS Protocol
-- ,C.custname AS Customer
,A.Study_Site
,CreatedBy
,CreatedByDate
,CustOrderNumber AS CustControlNum
,Order_Num AS FCSOrderNumber
,ShipByDate
,Customer
FROM OrderHistory OH (NOLOCK)
LEFT JOIN CDSWeb..Customer C (NOLOCK) ON C.custno = OH.CustNo collate SQL_Latin1_General_CP1_CI_AS
AND C.Site = OH.SiteID
INNER JOIN StatusesML ST (NOLOCK) on ST.StatusID = OH.StatusID
INNER JOIN Addresses A (NOLOCK) ON A.Address_ID = OH.AddressID
WHERE (WebUserID = ' + CAST(@WebUserID AS VARCHAR) + ' OR @AdminUser = 1 OR @UserCusts = 1 OR @UserProts = 1 )'

IF @StatusID <> '-1' --All Status Codes
BEGIN
--SET @SQL_WHERE = @SQL_WHERE + ' AND (ST.StatusID = CAST(@StatusID As VARCHAR))'
SET @SQL_SELECT = @SQL_SELECT + ' AND (ST.StatusID = ' + @StatusID + ')'
END

IF @Protocol <> '-1' --All Protocols
BEGIN
SET @SQL_SELECT = @SQL_SELECT + ' AND (OH.Protocol = ''' + @Protocol + ''')'
END

SET @SQL1 = @SQL_SELECT

PRINT @SQL1

EXEC(@SQL1)

END



Thanks for any help,

-S

TG
Master Smack Fu Yak Hacker

6065 Posts

Posted - 2009-09-18 : 13:57:13
It doesn't look to me like you even need dynamic sql for this statement. But I'm surprised you don't see the problem here since you have @WebUserID correct.

You need to treat @adminUser the same way as you did @WebUserID. That is cast the value and build it into the string.

WHERE (WebUserID = ' + CAST(@WebUserID AS VARCHAR) + ' OR @AdminUser = 1 OR

Either that or declare and SET @AdminUser within the dynamic sql code.

Be One with the Optimizer
TG
Go to Top of Page

sqlpal2009
Yak Posting Veteran

58 Posts

Posted - 2009-09-18 : 14:40:11
Thanks for the reply TG.

I tried both the options.

First,


WHERE (WebUserID = ' + CAST(@WebUserID AS VARCHAR) + ' OR ' + CAST(@AdminUser AS INT) + '= 1 OR ' + CAST(@UserCusts AS INT) + '= 1 OR ' + CAST(@UserProts AS INT) + '= 1 )'


got error: Conversion failed when converting the varchar value '

Second option

EXEC sp_executesql @SQL1, '@AdminUser INT ,@UserCusts INT ,@UserProts INT ' , @AdminUser ,@UserCusts ,@UserProts


Error:
Procedure expects parameter '@parameters' of type 'ntext/nchar/nvarchar'.

The @SQL1 is nvarchar(max).

Can you please tell me where I am wrong here?

I did not think of any other way than using dynamic sql for this SP. Please explain if you could have written it without dynamic SQL.

Thanks in advance,
-S
Go to Top of Page

TG
Master Smack Fu Yak Hacker

6065 Posts

Posted - 2009-09-18 : 14:53:19
The first error is because I guess you are trying to cast a varchar value to an INT. So just remove the CAST function if the variable is already varchar.

the sp_executesql error is probably becuase you simply need to add the unicode indicator on your @parameters parameter (the red N below):

EXEC sp_executesql @SQL1, N'@AdminUser INT ,@UserCusts INT ,@UserProts INT ' , @AdminUser ,@UserCusts ,@UserProts

And I assume you are already declareing and setting the actual parameters in blue

EDIT:
Duh, the first error is that the parameter is an INT but you need to convert it to varchar so you can concatenate the value into a string.

Be One with the Optimizer
TG
Go to Top of Page

sqlpal2009
Yak Posting Veteran

58 Posts

Posted - 2009-09-18 : 15:32:43
Thank you so much TG for your help.
Go to Top of Page
   

- Advertisement -