• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 349
  • Last Modified:

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

0
wrcplc
Asked:
wrcplc
  • 3
  • 2
  • 2
  • +1
5 Solutions
 
Raja Jegan RSQL 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]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 RSQL Server DBA & ArchitectCommented:
>> EXEC @string

As you call exec statement here it searches for a procedure next and throws out that error.
0
Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

 
Chandan_GowdaCommented:
declare @string as varchar
set @string = 'select * from [dbo].[expenseheader]'
EXEC (@string )
0
 
Chandan_GowdaCommented:
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

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

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.

  • 3
  • 2
  • 2
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now