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
 General SQL Server Forums
 New to SQL Server Programming
 dynamic sql issue

Author  Topic 

pnpsql
Posting Yak Master

246 Posts

Posted - 2012-05-09 : 06:19:57
i have a dynamic sql statement , @sqlcd that returns 5 rows and one col
i need to check

if @pass in(@sqlcd)
begin
primt 1

end


but it return same value if it exist in rowset or not

challenge everything

vijays3
Constraint Violating Yak Guru

354 Posts

Posted - 2012-05-09 : 06:30:50
For execution of dynamic sql need Exec or sp_executesql procedure .
It seems in your code @sqlcd is varible that contains the string .This is not the excution of dynamic sql.
Let us know your complete code and required output.so that we may review your code and provide some inputs
_
Go to Top of Page

pnpsql
Posting Yak Master

246 Posts

Posted - 2012-05-09 : 07:35:07
below is mu code .


declare @ploginname varchar(max)
declare @sqlcmd nvarchar(max)
declare @vid numeric(10)
declare @vpass varchar(max)


BEGIN

SET @PLOGINNAME = 'USER1';

SELECT @VID = PREVPASSNOTUSED
FROM QC_MASTER_FINAL.DBO.QM_POLICY_PARAMETER


set @vpass = 'SASASASAS8'

SET @SQLCMD = ';WITH CTE AS
(
SELECT PW_NO = 1, PASSWORD FROM QM_USER1 WHERE LOGINNAME = ' + '''' + @PLOGINNAME + ''''+' UNION ALL
SELECT PW_NO = 2, PASSWORD = PASSWORD1 FROM QM_USER1 WHERE LOGINNAME = ' + '''' + @PLOGINNAME + '''' + ' UNION ALL
SELECT PW_NO = 3, PASSWORD = PASSWORD2 FROM QM_USER1 WHERE LOGINNAME = ' + '''' + @PLOGINNAME + '''' + ' UNION ALL
SELECT PW_NO = 4, PASSWORD = PASSWORD3 FROM QM_USER1 WHERE LOGINNAME = ' + '''' + @PLOGINNAME + '''' + ' UNION ALL
SELECT PW_NO = 5, PASSWORD = PASSWORD4 FROM QM_USER1 WHERE LOGINNAME = ' + '''' + @PLOGINNAME + '''' + ' UNION ALL
SELECT PW_NO = 6, PASSWORD = PASSWORD5 FROM QM_USER1 WHERE LOGINNAME = ' + '''' + @PLOGINNAME + '''' + ' UNION ALL
SELECT PW_NO = 7, PASSWORD = PASSWORD6 FROM QM_USER1 WHERE LOGINNAME = ' + '''' + @PLOGINNAME + '''' + ' UNION ALL
SELECT PW_NO = 8, PASSWORD = PASSWORD7 FROM QM_USER1 WHERE LOGINNAME = ' + '''' + @PLOGINNAME + '''' + ' UNION ALL
SELECT PW_NO = 9, PASSWORD = PASSWORD8 FROM QM_USER1 WHERE LOGINNAME = ' + '''' + @PLOGINNAME + '''' + '
)
SELECT top ' + CAST(@VID as varchar(max)) + ' PASSWORD FROM CTE order by PW_NO ' ;

EXEC (@SQLCMD)

IF @VPASS NOT IN (@SQLCMD)
BEGIN

PRINT 'ok'

END


END





challenge everything
Go to Top of Page

sunitabeck
Master Smack Fu Yak Hacker

5155 Posts

Posted - 2012-05-09 : 07:45:58
The variable @SQLCMD contains the sql string that you have composed. It would not have the RESULTS of the query even after the EXEC (@SQLCMD).

When using dynamic SQL if you do want to return the results or variables of a query to the caller, you would need to use sp_executesql rather than EXEC. Sommarskog's blog has a nice explanation and examples: http://www.sommarskog.se/dynamic_sql.html

From looking at the code you posted, it seems like you don't need to use dynamic SQL. Is there a specific reason why you are trying to use dynamic SQL? Most experts advise avoiding the use of dynamic SQL if you are able to.
Go to Top of Page
   

- Advertisement -