472,331 Members | 1,786 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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

Convert variable into dynamic select

I am trying to assign @sql variable to @total, where @sql is a sql
statement stored on the database, however what I am getting is its
string value and not its calcuation. Could anybody help?
DECLARE my_cursor CURSOR FOR
SELECT sqlstatement from Sn_SalesReport
declare @sql varchar(255), @total varchar(20)
OPEN my_cursor
FETCH NEXT FROM my_cursor INTO @sql
-- Check @@FETCH_STATUS to see if there are any more rows to fetch.
WHILE @@FETCH_STATUS = 0
BEGIN
SET @total = (@sql)
print @total
-- This is executed as long as the previous fetch succeeds.
FETCH NEXT FROM my_cursor
INTO @sql
END
CLOSE my_cursor
DEALLOCATE my_cursor

I tried this SET @total = EXEC (@sql), but no success

Rod

Jul 23 '05 #1
9 6523

"Rodusa" <rc**********@yahoo.com> wrote in message
news:11*********************@f14g2000cwb.googlegro ups.com...
I am trying to assign @sql variable to @total, where @sql is a sql
statement stored on the database, however what I am getting is its
string value and not its calcuation. Could anybody help?
DECLARE my_cursor CURSOR FOR
SELECT sqlstatement from Sn_SalesReport
declare @sql varchar(255), @total varchar(20)
OPEN my_cursor
FETCH NEXT FROM my_cursor INTO @sql
-- Check @@FETCH_STATUS to see if there are any more rows to fetch.
WHILE @@FETCH_STATUS = 0
BEGIN
SET @total = (@sql)
print @total
-- This is executed as long as the previous fetch succeeds.
FETCH NEXT FROM my_cursor
INTO @sql
END
CLOSE my_cursor
DEALLOCATE my_cursor

I tried this SET @total = EXEC (@sql), but no success

Rod


Your syntax isn't correct - the best option is probably to use sp_executesql
with an output parameter. See this article for more details on dynamic SQL
generally:

http://www.sommarskog.se/dynamic_sql.html

And especially this section:

http://www.sommarskog.se/dynamic_sql.html#sp_executesql

Simon
Jul 23 '05 #2
Why do you want to put the SQL statements into a table? You'll have to
use sp_executesql to do this and put up with the performance, security
and maintenance issues inherent in dynamic SQL. I'm sure there ought to
be a better way to build a Sales Report!

--
David Portas
SQL Server MVP
--

Jul 23 '05 #3
David, The Sales Report is composed of several SQL statements. The
Stored procedure processes each row which contains a sql and places the
result in a temp table, in this case, SalesReport. I then use a asp.net
datagrid to view the results and export them to excel. The reason for
why I use the sql statetements separated is because the marketing dept.
should be able to update and add additional sql statements.

Rod

Jul 23 '05 #4
I tried your suggestion on the article. The problem is that I need to
open two databases at the same time. With the script below @total is
always outputing null and I can't get it to output the real values.
use sales
DECLARE my_cursor CURSOR FOR
SELECT sqlstatement from Sn_SalesReport
declare @sql nvarchar(512), @total decimal(19,4)
OPEN my_cursor
FETCH NEXT FROM my_cursor INTO @sql
WHILE @@FETCH_STATUS = 0
BEGIN
use testdb
EXEC sp_executesql @sql, N'@total decimal(19,4) OUTPUT', @total
OUTPUT
PRINT @total
use sales
update Sn_SalesReport set dollar_amount= @total
FETCH NEXT FROM my_cursor
INTO @sql
END
CLOSE my_cursor
DEALLOCATE my_cursor

Jul 23 '05 #5

"Rodusa" <rc**********@yahoo.com> wrote in message
news:11**********************@g14g2000cwa.googlegr oups.com...
I tried your suggestion on the article. The problem is that I need to
open two databases at the same time. With the script below @total is
always outputing null and I can't get it to output the real values.
use sales
DECLARE my_cursor CURSOR FOR
SELECT sqlstatement from Sn_SalesReport
declare @sql nvarchar(512), @total decimal(19,4)
OPEN my_cursor
FETCH NEXT FROM my_cursor INTO @sql
WHILE @@FETCH_STATUS = 0
BEGIN
use testdb
EXEC sp_executesql @sql, N'@total decimal(19,4) OUTPUT', @total
OUTPUT
PRINT @total
use sales
update Sn_SalesReport set dollar_amount= @total
FETCH NEXT FROM my_cursor
INTO @sql
END
CLOSE my_cursor
DEALLOCATE my_cursor


Try this:

exec testdb..sp_executesql @sql, N'@total decimal(19,4) OUTPUT', @total
OUTPUT

Simon
Jul 23 '05 #6
Thanks, Simon, I just changed it a little bit. I am almost there.
There is just one piece which is not working.

On the piece below I am trying to substitute "SELECT sum(total_amount)
AS total FROM testdb.dbo.invoice_hdr" with @sql but I am getting an
error:

Server: Msg 170, Level 15, State 1, Line 1
Line 1: Incorrect syntax near '@sql'.
Server: Msg 16950, Level 16, State 2, Line 17
The variable '@my_cur' does not currently have a cursor allocated to
it.

use sales
DECLARE my_cursor CURSOR FOR
SELECT sqlstatement from Sn_SalesReport
declare @sql nvarchar(512), @total decimal(19,4)
OPEN my_cursor
FETCH NEXT FROM my_cursor INTO @sql
-- Check @@FETCH_STATUS to see if there are any more rows to fetch.
WHILE @@FETCH_STATUS = 0
BEGIN

DECLARE @my_cur CURSOR
EXEC sp_executesql
N'SET @my_cur = CURSOR FOR SELECT sum(total_amount) AS total FROM
testdb.dbo.invoice_hdr; OPEN @my_cur',
N'@my_cur cursor OUTPUT', @my_cur OUTPUT
FETCH NEXT FROM @my_cur into @total
CLOSE @my_cur
DEALLOCATE @my_cur

update Sn_SalesReport set dollar_amount = @total
-- This is executed as long as the previous fetch succeeds.

FETCH NEXT FROM my_cursor
INTO @sql
END
CLOSE my_cursor
DEALLOCATE my_cursor

Jul 23 '05 #7

"Rodusa" <rc**********@yahoo.com> wrote in message
news:11*********************@z14g2000cwz.googlegro ups.com...
Thanks, Simon, I just changed it a little bit. I am almost there.
There is just one piece which is not working.

On the piece below I am trying to substitute "SELECT sum(total_amount)
AS total FROM testdb.dbo.invoice_hdr" with @sql but I am getting an
error:

Server: Msg 170, Level 15, State 1, Line 1
Line 1: Incorrect syntax near '@sql'.
Server: Msg 16950, Level 16, State 2, Line 17
The variable '@my_cur' does not currently have a cursor allocated to
it.


<snip>

I don't really understand your code - why is this line inside the cursor
loop?

update Sn_SalesReport set dollar_amount = @total

The value of dollar_amount will change every time you go through the loop,
and without a WHERE clause, every row will change (unless it's a one-row
table, of course). As a complete guess, you want something like this:

declare @sql nvarchar(512),
@total decimal(19,4),
@grand_total decimal(19,4)

set @grand_total = 0.0

DECLARE my_cursor CURSOR FOR
SELECT sqlstatement from Sn_SalesReport

OPEN my_cursor
FETCH NEXT FROM my_cursor INTO @sql
WHILE @@FETCH_STATUS = 0
BEGIN
EXEC sp_executesql @sql, N'@total decimal(19,4) OUTPUT', @total OUTPUT
set @grand_total = @grand_total + isnull(@total, 0.0)
FETCH NEXT FROM my_cursor INTO @sql
END
CLOSE my_cursor
DEALLOCATE my_cursor

update dbo.Sn_SalesReport
set dollar_amount = @grand_total
-- where ... ??

Here, you need @sql to look like this, to match the @total output parameter:

'select @total = sum(total_amount) from testdb.dbo.invoice_hdr'

I would also suggest - as David did - that there may be easier ways to
produce reports than writing your own reporting tool (which seems to be more
or less what you're doing). Nested cursors with dynamic SQL and user-defined
SQL statements create a number of fairly significant maintenance and
security issues. Going for a proper reporting tool will save you a lot of
time of effort in the longer run, although I appreciate that in the short
term any new solution will have a learning curve. SQL Server Reporting
Services is free if you have an MSSQL licence, for example:

http://www.microsoft.com/sql/reporting/default.asp

Simon
Jul 23 '05 #8
Simon, I am sorry, the update was just for testing. You are right, it
should show the update with the where clause like this: update
Sn_SalesReport set dollar_amount = @total where reportname=@reportname

I tried to match your suggestion, but @grand_total is being updated
with null value
I also will look look at SQL server reporting services as you
recommended.

use sales
DECLARE my_cursor CURSOR FOR
SELECT sqlstatement,reportname from Sn_SalesReport
declare @sql nvarchar(512), @total decimal(19,4),@grand_total
decimal(19,4), @reportname nvarchar(255)
OPEN my_cursor
FETCH NEXT FROM my_cursor INTO @sql,@reportname
WHILE @@FETCH_STATUS = 0
BEGIN
EXEC sp_executesql @sql, N'@total decimal(19,4) OUTPUT', @total
OUTPUT
set @grand_total = @grand_total + isnull(@total, 0.0)

update Sn_SalesReport set dollar_amount = @grand_total where
reportname=@reportname
FETCH NEXT FROM my_cursor INTO @sql,@reportname
END
CLOSE my_cursor
DEALLOCATE my_cursor

Jul 23 '05 #9
Rodusa (rc**********@yahoo.com) writes:
Simon, I am sorry, the update was just for testing. You are right, it
should show the update with the where clause like this: update
Sn_SalesReport set dollar_amount = @total where reportname=@reportname

I tried to match your suggestion, but @grand_total is being updated
with null value
I also will look look at SQL server reporting services as you
recommended.

use sales
DECLARE my_cursor CURSOR FOR
SELECT sqlstatement,reportname from Sn_SalesReport
declare @sql nvarchar(512), @total decimal(19,4),@grand_total
decimal(19,4), @reportname nvarchar(255)
OPEN my_cursor
FETCH NEXT FROM my_cursor INTO @sql,@reportname
WHILE @@FETCH_STATUS = 0
BEGIN
EXEC sp_executesql @sql, N'@total decimal(19,4) OUTPUT', @total
OUTPUT
set @grand_total = @grand_total + isnull(@total, 0.0)


This will indeed leave you with NULL in @grand_total. You need to
set @grand_total to 0 before you move into the cursor.

--
Erland Sommarskog, SQL Server MVP, es****@sommarskog.se

Books Online for SQL Server SP3 at
http://www.microsoft.com/sql/techinf...2000/books.asp
Jul 23 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Ken Halley | last post by:
How does one refer to a variable name using a variable within a variable name??? For example, I want to perform a SQL query and use the results of...
2
by: Martin Feuersteiner | last post by:
Hi I'm grateful for any light you can shed on this!! I've to admit, it's an unusual design but I've multiple contact tables named e.g....
7
by: Robert Brown | last post by:
Is there a way to use PreparedStatements (or bind variables) with SQL statements that have a variable number of arguments. For example, I have an...
4
by: Son KwonNam | last post by:
In XSLT, is this possible to get value from xml using XPath which is in XSLT variable? I mean XPath strings can be dynamic while XSL...
5
by: Allerdyce.John | last post by:
Do I need to convert string to integer in python? or it will do it for me (since dynamic type)? In my python script, I have this line: x /= 10;...
14
by: Matt | last post by:
I need to add the following variable into an SQL statement and not sure how to do it. strGCID needs to be inserted into the following statement:...
6
by: Muthu08 | last post by:
Hello Experts, I'm trying to convert an dynamic XML doc into HTML using XSLT. In the example I have shown one form(form1)...But there could...
0
by: becksinthecity | last post by:
I'm trying to convert the below SQL into Oracle but am having some issues with the variable declarations. SQL commands declare @sqlstring...
0
by: tickle | last post by:
Need to convert this PL/SQL script to Dynamic SQL Method 2 * copybook - celg02u3.sql SIR 24265 * * updates dt_deny...
0
better678
by: better678 | last post by:
Question: Discuss your understanding of the Java platform. Is the statement "Java is interpreted" correct? Answer: Java is an object-oriented...
0
by: teenabhardwaj | last post by:
How would one discover a valid source for learning news, comfort, and help for engineering designs? Covering through piles of books takes a lot of...
0
by: CD Tom | last post by:
This happens in runtime 2013 and 2016. When a report is run and then closed a toolbar shows up and the only way to get it to go away is to right...
0
by: Naresh1 | last post by:
What is WebLogic Admin Training? WebLogic Admin Training is a specialized program designed to equip individuals with the skills and knowledge...
0
jalbright99669
by: jalbright99669 | last post by:
Am having a bit of a time with URL Rewrite. I need to incorporate http to https redirect with a reverse proxy. I have the URL Rewrite rules made...
0
by: antdb | last post by:
Ⅰ. Advantage of AntDB: hyper-convergence + streaming processing engine In the overall architecture, a new "hyper-convergence" concept was...
0
by: Matthew3360 | last post by:
Hi there. I have been struggling to find out how to use a variable as my location in my header redirect function. Here is my code. ...
0
by: AndyPSV | last post by:
HOW CAN I CREATE AN AI with an .executable file that would suck all files in the folder and on my computerHOW CAN I CREATE AN AI with an .executable...
0
by: Arjunsri | last post by:
I have a Redshift database that I need to use as an import data source. I have configured the DSN connection using the server, port, database, and...

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.