Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Help crerating sa loop

Posted on 2013-01-16
10
Medium Priority
?
158 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
Back Up Your Microsoft Windows Server®

Back up all your Microsoft Windows Server – on-premises, in remote locations, in private and hybrid clouds. Your entire Windows Server will be backed up in one easy step with patented, block-level disk imaging. We achieve RTOs (recovery time objectives) as low as 15 seconds.

 

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

Moving data to the cloud? Find out if you’re ready

Before moving to the cloud, it is important to carefully define your db needs, plan for the migration & understand prod. environment. This wp explains how to define what you need from a cloud provider, plan for the migration & what putting a cloud solution into practice entails.

Question has a verified solution.

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

For both online and offline retail, the cross-channel business is the most recent pattern in the B2C trade space.
It is possible to export the data of a SQL Table in SSMS and generate INSERT statements. It's neatly tucked away in the generate scripts option of a database.
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…
Using examples as well as descriptions, and references to Books Online, show the documentation available for date manipulation functions and by using a select few of these functions, show how date based data can be manipulated with these functions.

721 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