Solved

Help crerating sa loop

Posted on 2013-01-16
10
151 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 142

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 142

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
 

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 142

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 500 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
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 

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 142

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

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Sharepoint 2010 Product Configuration Wizard fails 4 24
encyps queries mssql 15 27
login and database user 3 22
Update in Sql 7 12
In this article I will describe the Backup & Restore method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
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…
This videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function
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.

743 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now