Solved

execute a string

Posted on 2009-07-08
8
324 Views
Last Modified: 2012-05-07
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
Comment
Question by:wrcplc
  • 3
  • 2
  • 2
  • +1
8 Comments
 
LVL 57

Accepted Solution

by:
Raja Jegan R earned 110 total points
ID: 24801875
Hope this helps:
declare @string nvarchar(1000);

set @string = 'select * from [dbo].[expenseheader]';

EXEC sp_executesql @string;

Open in new window

0
 
LVL 142

Assisted Solution

by:Guy Hengel [angelIII / a3]
Guy Hengel [angelIII / a3] earned 70 total points
ID: 24801878
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
 
LVL 57

Assisted Solution

by:Raja Jegan R
Raja Jegan R earned 110 total points
ID: 24801883
>> EXEC @string

As you call exec statement here it searches for a procedure next and throws out that error.
0
 
LVL 7

Assisted Solution

by:Chandan_Gowda
Chandan_Gowda earned 70 total points
ID: 24801890
declare @string as varchar
set @string = 'select * from [dbo].[expenseheader]'
EXEC (@string )
0
Complete VMware vSphere® ESX(i) & Hyper-V Backup

Capture your entire system, including the host, with patented disk imaging integrated with VMware VADP / Microsoft VSS and RCT. RTOs is as low as 15 seconds with Acronis Active Restore™. You can enjoy unlimited P2V/V2V migrations from any source (even from a different hypervisor)

 
LVL 7

Assisted Solution

by:Chandan_Gowda
Chandan_Gowda earned 70 total points
ID: 24801897
you dont have to change anything...Just add brackets
0
 
LVL 57

Expert Comment

by:Raja Jegan R
ID: 24801910
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
 

Author Closing Comment

by:wrcplc
ID: 31601011
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
 
LVL 142

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 24801944
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

Zoho SalesIQ

Hassle-free live chat software re-imagined for business growth. 2 users, always free.

Join & Write a Comment

Introduction: When running hybrid database environments, you often need to query some data from a remote db of any type, while being connected to your MS SQL Server database. Problems start when you try to combine that with some "user input" pass…
In this article I will describe the Copy Database Wizard method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

746 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now