Solved

Help crerating sa loop

Posted on 2013-01-16
10
153 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
Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

 

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
 

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

Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

Question has a verified solution.

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

Suggested Solutions

Occasionally there is a need to clean table columns, especially if you have inherited legacy data. There are obviously many ways to accomplish that, including elaborate UPDATE queries with anywhere from one to numerous REPLACE functions (even within…
Slowly Changing Dimension Transformation component in data task flow is very useful for us to manage and control how data changes in SSIS.
Via a live example, show how to extract information from SQL Server on Database, Connection and Server properties
Via a live example, show how to extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.

773 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