473,569 Members | 2,991 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 ' + @retrievedTable Name
)

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 9055
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 ' + @retrievedTable Name
)

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**********@ya hoo.com (Steve) wrote in message news:<6f******* *************** ****@posting.go ogle.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 ' + @retrievedTable Name
)

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,
@retrievedTable Name sysname,
@sql nvarchar(4000)
set @retrievedTable Name = 'sysobjects'
set @sql = N'select @rec_count = count(*) from ' + @retrievedTable Name
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.go ogle.com>...
st**********@ya hoo.com (Steve) wrote in message news:<6f******* *************** ****@posting.go ogle.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 ' + @retrievedTable Name
)

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,
@retrievedTable Name sysname,
@sql nvarchar(4000)
set @retrievedTable Name = 'sysobjects'
set @sql = N'select @rec_count = count(*) from ' + @retrievedTable Name
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
11936
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 like array arrayListOfTablesToProcess = { "orders", "phone",
4
8065
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 creates a string of custom SQL statement and returns this string back to the main stored procedure. This SQL statements work fine on there own....
5
1379
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 code. for example strMessage = "Dear " & rs("firstname")
4
3793
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 the where clause of a sql string to send to SQL 2000 for a data set. Currenly I check each control with an IF statement to determine if something...
16
5066
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 information. I would guess to combine the selected items like so: "6,4,8,19,2". Kind of do the following: INSERT into tblUser (fields) VALUES...
5
12245
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 to the database. SQLSTATE=57030. Background: I created a linked server to DB2 8.1 database which called GRR_DB2Server. In my stored procedure...
8
1449
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 that all SQL work should be completed by the SQL server, therefore use stored procedures even for the simplest of select statements. Does this...
1
2749
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 visual basic code and have it loop thru and add the various where statements. I am new to SQL server so excuse me if I don't have the terminology...
1
7048
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. ============================================== PROCEDURE SaveTestAs(P_ProgramID NUMBER, P_ProgramName VARCHAR2) ...
0
7703
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7618
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
1
7679
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
7983
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
5223
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3657
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3647
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2117
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1228
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.