Solved

execute a string

Posted on 2009-07-08
8
325 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

VMware Disaster Recovery and Data Protection

In this expert guide, you’ll learn about the components of a Modern Data Center. You will use cases for the value-added capabilities of Veeam®, including combining backup and replication for VMware disaster recovery and using replication for data center migration.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
index  - last use and update 8 56
Need help creating a stored procedure 4 56
insert query with value having 's 2 46
Get row count of current SQL query 8 44
I am showing a way to read/import the excel data in table using SQL server 2005... Suppose there is an Excel file "Book1" at location "C:\temp" with column "First Name" and "Last Name". Now to import this Excel data into the table, we will use…
Introduction This article will provide a solution for an error that might occur installing a new SQL 2005 64-bit cluster. This article will assume that you are fully prepared to complete the installation and describes the error as it occurred durin…
This tutorial demonstrates a quick way of adding group price to multiple Magento products.
This is a video that shows how the OnPage alerts system integrates into ConnectWise, how a trigger is set, how a page is sent via the trigger, and how the SENT, DELIVERED, READ & REPLIED receipts get entered into the internal tab of the ConnectWise …

943 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

16 Experts available now in Live!

Get 1:1 Help Now