Solved

Help crerating sa loop

Posted on 2013-01-16
10
156 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

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 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 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

Transaction Monitoring Vs. Real User Monitoring

Synthetic Transaction Monitoring Vs. Real User Monitoring: When To Use Each Approach? In this article, we will discuss two major monitoring approaches: Synthetic Transaction and Real User Monitoring.

Question has a verified solution.

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

Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
I have a large data set and a SSIS package. How can I load this file in multi threading?
Familiarize people with the process of utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…
This video shows, step by step, how to configure Oracle Heterogeneous Services via the Generic Gateway Agent in order to make a connection from an Oracle session and access a remote SQL Server database table.

734 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