473,327 Members | 2,081 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,327 software developers and data experts.

Building dynamic tsql statements in a loop

Hi;

I would like to read a list of tables from a temp table and then do a
sql statement on each table name retrieved in a loop, ie:

-- snip cursor loop where cursor contains a list of tables

declare @rec_count int
set @rec_count = 0
exec('select @rec_count = count(myfield) from ' + @retrievedTableName
)

This does not work. SQLSERVER tells me @rec_count is not declared.

How can I get the @rec_count populated....or can I?

Thanks in advance

Steve
Jul 20 '05 #1
3 9034
Steve wrote:
Hi;

I would like to read a list of tables from a temp table and then do a
sql statement on each table name retrieved in a loop, ie:

-- snip cursor loop where cursor contains a list of tables

declare @rec_count int
set @rec_count = 0
exec('select @rec_count = count(myfield) from ' + @retrievedTableName
)

This does not work. SQLSERVER tells me @rec_count is not declared.

How can I get the @rec_count populated....or can I?

Thanks in advance

Steve


From memory, use the sp_executesql (sp_execsql?) system stored
procedure, that will enable you to have an output parameter.

For this particular instance you might (again if memory serves) simply use:

set @rec_count = exec('select count(*) from ' + @...)

although you may want to build your sql string into a delcared varchar
variable first and stick that into the brackets as I've had problems
using dynamic sql this way and it will aid debugging as your string
concatenation and dynamic sql will be on separate lines of code.

--
Error reading sig - A)bort R)etry I)nfluence with large hammer
Jul 20 '05 #2
st**********@yahoo.com (Steve) wrote in message news:<6f**************************@posting.google. com>...
Hi;

I would like to read a list of tables from a temp table and then do a
sql statement on each table name retrieved in a loop, ie:

-- snip cursor loop where cursor contains a list of tables

declare @rec_count int
set @rec_count = 0
exec('select @rec_count = count(myfield) from ' + @retrievedTableName
)

This does not work. SQLSERVER tells me @rec_count is not declared.

How can I get the @rec_count populated....or can I?

Thanks in advance

Steve


Here is one way:

declare @rec_count int,
@retrievedTableName sysname,
@sql nvarchar(4000)
set @retrievedTableName = 'sysobjects'
set @sql = N'select @rec_count = count(*) from ' + @retrievedTableName
exec sp_executesql @sql, N'@rec_count int output', @rec_count OUTPUT
select @rec_count

Simon
Jul 20 '05 #3
sq*@hayes.ch (Simon Hayes) wrote in message news:<60**************************@posting.google. com>...
st**********@yahoo.com (Steve) wrote in message news:<6f**************************@posting.google. com>...
Hi;

I would like to read a list of tables from a temp table and then do a
sql statement on each table name retrieved in a loop, ie:

-- snip cursor loop where cursor contains a list of tables

declare @rec_count int
set @rec_count = 0
exec('select @rec_count = count(myfield) from ' + @retrievedTableName
)

This does not work. SQLSERVER tells me @rec_count is not declared.

How can I get the @rec_count populated....or can I?

Thanks in advance

Steve


Here is one way:

declare @rec_count int,
@retrievedTableName sysname,
@sql nvarchar(4000)
set @retrievedTableName = 'sysobjects'
set @sql = N'select @rec_count = count(*) from ' + @retrievedTableName
exec sp_executesql @sql, N'@rec_count int output', @rec_count OUTPUT
select @rec_count

Simon


Thanks Simon, you just saved me from a lot of tedious work.

Steve
Jul 20 '05 #4

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

Similar topics

2
by: Steve | last post by:
Hi; I have been writing a lot of short tsql scripts to fix a lot of tiny database issues. I was wondering if a could make an array of strings in tsql that I could process in a loop, something...
4
by: MD | last post by:
I am trying to create a dynamic SQL statement to create a view. I have a stored procedure, which based on the parameters passed calls different stored procedures. Each of this sub stored procedure...
5
by: JP SIngh | last post by:
Hi All Hope someone can help us with an issue here. We use an ASP application in which we send out emails using cdo for various events. At present the email text is hard coded into the...
4
by: Brian Shannon | last post by:
I have 3 combo boxes and two date text boxes on a .aspx page. The user can fill in any of the 5 controls or none to filter a datagrid. I was hoping someone could explain how to efficiently build...
16
by: David Lozzi | last post by:
Hello, I have some code that adds a new user. The new user has a checkboxlist of items which they can be associated with. I would like to send this list of items to TSQL along with the new user...
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...
8
by: David Lozzi | last post by:
I'm fairly new to ASP.Net 2.0 SQLDatasource objects. It defaults using TSQL statments for the SELECT, INSERT, UPDATE, DELETE commands, which is great and it works. However, I've always been taught...
1
by: marlind | last post by:
I want to be able to build a query with a variable amount of where statements. A user will be adding various conditions. Is there a way to make a query flexible? In Microsoft access I would write...
1
by: atp2k3 | last post by:
Hello all, I am new to convert PLSQL to TSQL. Any expertise in SQL, please help me to do convert the belowed PLSQL procedure to SP in SQL server. I really appreciate your time. Thanks in advance....
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
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...

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.