Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Help crerating sa loop

Posted on 2013-01-16
10
Medium Priority
?
159 Views
Last Modified: 2013-03-20
I need to run a series of queries that will do the following

1. Select * into TEMP_table_name from  openquery(NEW,' select * from table_name where trans_date = 20050102 and trans_date < 20050103')

2. Append * into table_name from temp_table_name

3. IF OBJECT_ID('dbo.temp_table_name', 'U') IS NOT NULL
  DROP TABLE dbo.temp_table_name

4. Go to next query (eg 20050103 - 20050104)


Whats the best way to handle this as I need to do appx 2900 itierations of this date range
0
Comment
Question by:hwassinger
  • 6
  • 4
10 Comments
 
LVL 143

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 38783524
you can do this with a loop + dynamic sql.
this would be the start to execute the single statement with the values you posted, you can put variables as needed.
 declare @sql varchar(2000)
set @sql = 'Select * into TEMP_table_name from  openquery(NEW, '' select * from table_name where trans_date >= ' + '20050102' + ' and trans_date < ' + '20050103' + '' ) '
exec(@sql) 

Open in new window

0
 

Author Comment

by:hwassinger
ID: 38783569
I dont see how thats going to do it. I have the 2900 query steps written by using excel to concatenate them.

Its the loop step and the append that I am struggling with.

If I do it manually I get "There is already an object named 'table_name in the daatabase.

Ideally Im thinking something like.

query1
macro that appends then drops
query2
macro that appends then drops

(The little I know about SQL im thinking the macro = a stored procedure?)

If I can get the append syntax correct that will be a big step while I google making a stored procedure
0
 
LVL 143

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 38783598
> I have the 2900 query steps
if this is for a sequence of dates, we can do it like this:

declare @sql varchar(2000)
declare @min_date date
declare @max_date date

-- example dates:
set @min_date = convert(datetime, '2005-01-02', 120)
set @max_date = convert(datetime, '2012-01-01', 120)

-- create the temp table once
Select * into TEMP_table_name from  openquery(NEW,' select * from table_name where 1 = 0 ')

while @min_date < @max_date
begin

  set @sql = 'insert into TEMP_table_name select * from  openquery(NEW, '' select * from table_name where trans_date >= ' + convert(varchar(8),  @min_date, 112 ) + ' and trans_date < ' + convert(varchar(8),  @min_date + 1, 112 ) + '' ) '
  exec(@sql) 
  
 set @min_date = dateadd(day, 1, @min_date)
 insert into  table_name from temp_table_name
 delete TEMP_table_name

end --- end loop
 DROP TABLE dbo.temp_table_name

Open in new window

0
What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

 

Author Comment

by:hwassinger
ID: 38783702
That looks like what I am trying to do (turns green with envy)


Returns the following
Msg 102, Level 15, State 1, Line 15
Incorrect syntax near ')'.
Msg 105, Level 15, State 1, Line 15
Unclosed quotation mark after the character string '
  exec(@sql)
 
 set @min_date = dateadd(day, 1, @min_date)
 insert into  table_name from temp_table_name
 delete TEMP_table_name

end --- end loop
 DROP TABLE dbo.temp_table_name
'.
0
 
LVL 143

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 1500 total points
ID: 38783996
I see indeed a missing quote in my code
declare @sql varchar(2000)
declare @min_date date
declare @max_date date

-- example dates:
set @min_date = convert(datetime, '2005-01-02', 120)
set @max_date = convert(datetime, '2012-01-01', 120)

-- create the temp table once
Select * into TEMP_table_name from  openquery(NEW,' select * from table_name where 1 = 0 ')

while @min_date < @max_date
begin

  set @sql = 'insert into TEMP_table_name select * from  openquery(NEW, '' select * from table_name where trans_date >= ' + convert(varchar(8),  @min_date, 112 ) + ' and trans_date < ' + convert(varchar(8),  @min_date + 1, 112 ) + ' '' ) '
  exec(@sql) 
  
 set @min_date = dateadd(day, 1, @min_date)
 insert into  table_name from temp_table_name
 delete TEMP_table_name

end --- end loop
 DROP TABLE dbo.temp_table_name
                                            

Open in new window

0
 

Author Comment

by:hwassinger
ID: 38784625
OK I am getting

Msg 156, Level 15, State 1, Line 19
Incorrect syntax near the keyword 'from'.
 seems to be related to "insert into  table_name from temp_table_name"
0
 

Author Comment

by:hwassinger
ID: 38784992
OK my research indicates that we cannot do a bulk copy from a table into an esxisrting copy but rather have to name the columns to ensure data integrity.

I created the insert into statement and that statement works fine if executed by itself.

I now am receiving

Msg 206, Level 16, State 2, Line 15
Operand type clash: date is incompatible with int
0
 

Author Comment

by:hwassinger
ID: 38785017
OK continuing ot research this... my yyyymmdd is stored as text, it looks like that may be causing some difficulty
0
 
LVL 143

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 38785976
>OK continuing ot research this... my yyyymmdd is stored as text,
in which table?
you may need to add a convert() function ...
0
 

Author Comment

by:hwassinger
ID: 38856084
What wiould that syntax look like?
0

Featured Post

NFR key for Veeam Backup for Microsoft Office 365

Veeam is happy to provide a free NFR license (for 1 year, up to 10 users). This license allows for the non‑production use of Veeam Backup for Microsoft Office 365 in your home lab without any feature limitations.

Question has a verified solution.

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

Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
When trying to connect from SSMS v17.x to a SQL Server Integration Services 2016 instance or previous version, you get the error “Connecting to the Integration Services service on the computer failed with the following error: 'The specified service …
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.
Using examples as well as descriptions, and references to Books Online, show the documentation available for datatypes, explain the available data types and show how data can be passed into and out of variables.

916 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