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
 sp_executesql problem

Author  Topic 

bill_
Starting Member

38 Posts

Posted - 2010-05-19 : 15:18:33
This calls a stored procedure:
exec sp1 @param1='test'

Getting error "must decl scalar var @param1", when I try this in sp1:
declare @q
set @q='select cname,cno from tblnames where cname=@param1'
exec sp_executesql @q

What am I doing wrong ?

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2010-05-19 : 15:21:44
Here's an example of how to properly use it:

SET @sql = '
SELECT @indexName = [name], @disabled = is_disabled, @allowPageLocks = allow_page_locks
FROM ' + QUOTENAME(@dbName) + '.sys.indexes
WHERE [object_id] = @objectId AND index_id = @indexId'

SET @parmDef = N'
@objectId int, @indexId int, @indexName sysname OUTPUT,
@disabled bit OUTPUT, @allowPageLocks bit OUTPUT'

EXEC sp_executesql
@sql, @parmDef, @objectId = @objectId, @indexId = @indexId,
@indexName = @indexName OUTPUT, @disabled = @disabled OUTPUT,
@allowPageLocks = @allowPageLocks OUTPUT


This is directly from code that I have in production.

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog
Go to Top of Page

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2010-05-19 : 15:23:44
If you don't need anything outputted from the dynamic query, then you just need this:
set @q='select cname,cno from tblnames where cname=' + @param1

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog
Go to Top of Page

bill_
Starting Member

38 Posts

Posted - 2010-05-19 : 15:37:16
Sorry-was out for minute. Tried 2nd way while waiting and got invalid col name 'test'.
Will try top way.
Go to Top of Page

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2010-05-19 : 15:47:11
TES doesn't even exist in your posted code, so you'll need to provide us with the actual code if you want us to help you.

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog
Go to Top of Page

bill_
Starting Member

38 Posts

Posted - 2010-05-19 : 16:03:07
Sorry about that, meant 'test'. Editted that post. Only thing diff from origianl post was line
set @q='select cname,cno from tblnames where cname='+@param1
whcich got error "invalid col name 'test'"

Trying to immitate the original way you suggested now.
Go to Top of Page

vijayisonly
Master Smack Fu Yak Hacker

1836 Posts

Posted - 2010-05-19 : 16:12:02
change it to...
set @q='select cname,cno from tblnames where cname='+ '''' + @param1 + ''''
Go to Top of Page

bill_
Starting Member

38 Posts

Posted - 2010-05-19 : 16:23:17
Thank you tkizer and vijayisonly.
Will keep trying to understand the 1st tkizer exampl and for this first step, the vijayisonly example worked.

What do the ''''s mean ?
Go to Top of Page

vijayisonly
Master Smack Fu Yak Hacker

1836 Posts

Posted - 2010-05-19 : 16:28:19
Since @param1 is really a value in the table you are serching against, you need to enclose it with single quotation mark (') and thats what I've done.

do a
PRINT @q
after your SET statement...and you should see what it does.
Go to Top of Page

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2010-05-19 : 16:35:30
Oops, yes you need single quotes to enclose the string!

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog
Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2010-05-20 : 09:12:32
Refer this to know how to use single quotes in dynamic sql
http://beyondrelational.com/blogs/madhivanan/archive/2008/02/19/understanding-single-quotes.aspx

Madhivanan

Failing to plan is Planning to fail
Go to Top of Page
   

- Advertisement -