473,383 Members | 1,815 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,383 software developers and data experts.

How to Execute a Query Stored in a variable in Stored Procedure

I want to exicute a Query that stored in a variable. and the Query is used to get a given value to a variable. I tried to use exec command but that returns an error. i cant get an idea for how to do
the SQL commands are given bellow

This line is used to generate the required query and stored to a variable @mnvcSQL
select @mnvcSQL='Select @Prev=year' + convert(varchar,cast((@Field-1) as int))+ ' from tblFinancialDistribution where ProjectId=' + cast(@pintProjectId as Varchar)+ ' and [lineno]=' + cast(@mvcLineNoNetCashFlow as Varchar)

After Executing the above line the variable @mnvcSQL contain the the SQL Sentance like
Select @Prev=year4 from tblFinancialDistribution where ProjectId=42 and [lineno]=-29

I Want to exicute this statement and want the resulted value in @Prev
Jan 31 '07 #1
12 37039
iburyak
1,017 Expert 512MB
I don't think you can return parameter from exec command.
But as I looked at your query closely I don’t see a need to concatenate this statement to be later executed.
Is it possible to use it as is and have @Prev returned its normal way?

[PHP]Select @Prev = year + (@Field - 1)
from tblFinancialDistribution where ProjectId = @pintProjectId
and [lineno]= @mvcLineNoNetCashFlow[/PHP]
Jan 31 '07 #2
i am creating this query for stisfy my condition. the problem inside is this. i want to select a field thats name started with year. the year 1 to year 15 columns are in my table. some time i want year1 and some time i want year7. that is dynamic. the only solution is to concatinate the statement.
and the final query is stored in a variable. that query returns a value. i want to exicute the query and get the result in another variable.
Feb 2 '07 #3
the above statement consider year as the column name and @field1 is the variable that must be added to the value of year. so pls check it, help me
Feb 2 '07 #4
iburyak
1,017 Expert 512MB
So I think you can start concatenation after you've got your variable result.


Example:
[PHP]Declare @Prev int, @SQL varchar(8000)

Select @Prev = year + (@Field - 1)
from tblFinancialDistribution where ProjectId = @pintProjectId
and [lineno]= @mvcLineNoNetCashFlow

Select @SQL = 'select ' + convert(varchar(10) + 'column_name from table_name'

Execute @SQL[/PHP]
Feb 2 '07 #5
Execute @SQL Not working. The Exicute statement is used to exicute stored procedures not queries.
I solve the above problem by using temparary table (#Table). if any one have a different solution please replay
Feb 5 '07 #6
iburyak
1,017 Expert 512MB
I think I forgot to put ( )around @SQL


Try this example:

[PHP]
Declare @SQL varchar(4000)

Select @SQL = 'Select * from sysobjects'

EXEC (@SQL)[/PHP]
Feb 5 '07 #7
i think you did't get my question...
Please Check this Query and tell me where is the mistake
Expand|Select|Wrap|Line Numbers
  1. declare @a int
  2. declare @str varchar(1000)
  3. declare @b numeric(18,3)
  4.  
  5. select @a=1
  6. select @str ='select @b=year' + convert(varchar(1),@a) + ' from tblpandlsummary'
  7. select @str
  8. execute (@str)
  9. select @b
Feb 6 '07 #8
iburyak
1,017 Expert 512MB
i think you did't get my question...
Please Check this Query and tell me where is the mistake
Expand|Select|Wrap|Line Numbers
  1. declare @a int
  2. declare @str varchar(1000)
  3. declare @b numeric(18,3)
  4.  
  5. select @a=1
  6. select @str ='select @b=year' + convert(varchar(1),@a) + ' from tblpandlsummary'
  7. select @str
  8. execute (@str)
  9. select @b

I think I did get your question.
You just don't understand how it works. You can't expose variable result outside execute statement. The only way I see it would work is:

[PHP]declare @a int
declare @str varchar(1000)


select @a=1
select @str ='declare @b numeric(18,3) select @b=year' + convert(varchar(1),@a) + ' from tblpandlsummary select @b'
select @str
execute (@str)[/PHP]

But I don't see a point. You can't use it after statement is executed.
Feb 6 '07 #9
almaz
168 Expert 100+
i think you did't get my question...
OK, it's my turn to step into discussion :).
As you didn't provide a DDL for your original question, I'll show you a sample that will do a trick (note that @sql must be nvarchar, not varchar):
Expand|Select|Wrap|Line Numbers
  1. declare @sql nvarchar(2000)
  2. select top 1 @sql = 'select top 1 @Value = ID from [' + object_name(id) + ']' 
  3. from syscolumns 
  4. where name = 'ID' and xtype = 56 -- int column
  5.  
  6. declare @MyValue int
  7. exec sp_executesql @sql, N'@Value int output', @Value = @MyValue output
  8.  
  9. print @MyValue
Feb 6 '07 #10
iburyak
1,017 Expert 512MB
Cool.... :)
Feb 6 '07 #11
Yesssssss... Thats the final solution....
Feb 7 '07 #12
This worked great for me. Thanks!
Feb 11 '10 #13

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: PJ | last post by:
<% Set Conn = Server.CreateObject("ADODB.Connection") Conn.open application("dtat_motor_connectionstring") set rs = new adodb.recordset 'Set RS = Conn.Execute(' "exec spcn_update_transactions &...
2
by: Matt | last post by:
I want to exexute stored procedure in ASP, but it has error "Microsoft VBScript compilation (0x800A0401) Expected end of statement" on line (1). The stored procedure "sp_emp" contain "select *...
10
by: Thomas R. Hummel | last post by:
I have a stored procedure that suddenly started performing horribly. The query plan didn't look right to me, so I copy/pasted the code and ran it (it's a single SELECT statement). That ran pretty...
2
by: Noloader | last post by:
Hello, Access XP, SQL Server 2000 Is it possible to hide a SP under Queries in Access, yet still be able to Execute it from Access? (Similar to hiding Tables, then using Views) We hooked...
2
by: Eli | last post by:
Hi all We currently have a strange problem with calling a Stored Procedure (SQL Database) in our C# Project. The only error I get is "System error" which says a lot :) Background: We have...
7
by: JIM.H. | last post by:
Hello, Is there any difference to between SLQ string in the code and call execute query and call a stored procedure and execute the query that way concerning speed, effectiveness, reliability,...
0
by: franjorge | last post by:
Hi, I have created two stored procedures via VB using this code: sql = "CREATE PROC " & nombre_proc & " AS SELECT *" & _ " From MBM_PUNTOS_SCE_SIN_COINCIDIR_SIEGE_FALTA_PM_NE_" & mes & _ "...
1
by: Crash | last post by:
Hi, ..NET v1.x SP1 VS 2003 SQL Server 2000 SP3 Server 2000, XP, Server 2003 I would like to programmatically execute {possibly many} SQL Server batch scripts. Aka I have many scripts that...
5
by: alingsjtu | last post by:
Hello, every body. When execute dynamic generated multiple OPENQUERY statements (which linkes to DB2) in SQLServer, I always got SQL1040N The maximum number of applications is already connected...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.