execute a string

I am trying to create a string dynamically and then get it executed.  My problem is that the string may be 100 chars long or up to 600 chars.  I can get the sql to create the srting in the correct syntax.  I have used a very simple of select in order to make it easy

my problem is that the code below Parses Ok but when I execute it says

Msg 2812, Level 16, State 62, Line 5
Could not find stored procedure 's'.

This is confusing as I am not trying to create a SP

Also, is there specific syntax that I am missing

Any ideas please

Thanks from the novice

Adrian
declare @string as varchar
 
set @string = 'select * from [dbo].[expenseheader]'
 
EXEC @string

Open in new window

wrcplcAsked:
Who is Participating?
 
Raja Jegan RConnect With a Mentor SQL Server DBA & ArchitectCommented:
Hope this helps:
declare @string nvarchar(1000);
set @string = 'select * from [dbo].[expenseheader]';
EXEC sp_executesql @string;

Open in new window

0
 
Guy Hengel [angelIII / a3]Connect With a Mentor Billing EngineerCommented:
2 errors: varchar must be size specified
exec (string), oitherwise string is considered a stored proc
declare @string as varchar(200)
 
set @string = 'select * from [dbo].[expenseheader]'
 
EXEC(@string)

Open in new window

0
 
Raja Jegan RConnect With a Mentor SQL Server DBA & ArchitectCommented:
>> EXEC @string

As you call exec statement here it searches for a procedure next and throws out that error.
0
Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
Chandan_GowdaConnect With a Mentor Commented:
declare @string as varchar
set @string = 'select * from [dbo].[expenseheader]'
EXEC (@string )
0
 
Chandan_GowdaConnect With a Mentor Commented:
you dont have to change anything...Just add brackets
0
 
Raja Jegan RSQL Server DBA & ArchitectCommented:
angelIII,

I suggested using sp_executesql since this syntax EXEC(@string) will be deprecated after 2005
And string should be declared as nvarchar instead of varchar.

Kindly correct if I am wrong.
0
 
wrcplcAuthor Commented:
Thank you all.  So many so quick.

Sorry to have taken your time but feel safe in the knowledge you have all saved me from hours of research

Again, thanks

adrian
0
 
Guy Hengel [angelIII / a3]Billing EngineerCommented:
Hi,
>I suggested using sp_executesql since this syntax EXEC(@string) will be deprecated after 2005
new to me, but good to know.
you could have put that in the first comment :)

>And string should be declared as nvarchar instead of varchar.
for sp_executesql: yes, required.
for exec: not required

for  the rest, I explained the 2 original issues with the code in my comment.

a3
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.