|
|
|
Grasshopper
      
Group: General Forum Members
Last Login: Monday, April 22, 2013 12:46 PM
Points: 14,
Visits: 61
|
|
Hello everyone, I'm stumped right now. Here is the deal, I need to replicate data from one table to another (basically archiving data) but there is a TON of logic happening before the archiving query (shown below) is called. To give you some background, I am dynamically keeping my local table schemas mirrored to the external tables I am pulling down. Some of these tables have over 500 columns (yes I am getting that warning about exceeding my limit). Either way, the reason I share this is that dyncamic SQL is not going to work, because I can't fit the insert statement into a variable due to the amount of columns.
Now I have performed an sp_columns on BOTH table1 and table2, and they are EXACTLY the same... obviously table 1 has a column that table 2 doesnt (Record_Change_Date) and that column is set as a DateTime. Either way, when I run this, I get the "Conversion failed" error.
Any ideas??
--------- CODE ------------
Declare @changeDate As DateTime
Set @changeDate = GETDATE()
Insert Into [Table1] Select *, Convert(DateTime, @changeDate) As [Record_Change_Date] From [Table2] Where 1=1 And Record_ID NOT IN (Select Record_ID From Table1)
|
|
|
|
|
SSC Eights!
      
Group: General Forum Members
Last Login: Yesterday @ 4:59 PM
Points: 960,
Visits: 1,924
|
|
|
|
|
|
Grasshopper
      
Group: General Forum Members
Last Login: Monday, April 22, 2013 12:46 PM
Points: 14,
Visits: 61
|
|
Yes... I was trying everything possible. I originally didn't do anything with it.. it used to be select *, @changeDate As [Record_Change_Date]
So I tried it just using the var, I tried casting it, I tried converting it. I went to the source table and made sure that every DateTime field in the source table returned 1 when the IsDate() function was called. (which meant getting rid of all the nulls).
So now every datetime field in the source table returns 1 when is checked against the IsDate function.
I have no idea why this is happening, and if this doesn't work, I'll need to rework the whole thing, which I really dont want to do.
|
|
|
|
|
SSC Rookie
      
Group: General Forum Members
Last Login: Monday, May 20, 2013 4:33 AM
Points: 25,
Visits: 112
|
|
Hi,
Execute the following on both the tables and paste the output sp_help 'tableA'
Thanks Satyen
|
|
|
|
|
SSC Rookie
      
Group: General Forum Members
Last Login: Monday, May 20, 2013 4:33 AM
Points: 25,
Visits: 112
|
|
also Please try executing the query without 'where 1=1' statement
Thanks Satyen
|
|
|
|
|
Grasshopper
      
Group: General Forum Members
Last Login: Monday, April 22, 2013 12:46 PM
Points: 14,
Visits: 61
|
|
LOL I dont know why everyone gets cross-eyed at the 1=1 (it's just a syntax used for easier troubleshooting and it's ignored by the query engine, so no overhead)
I've attached the output of sp_help to this post.
Oh and just FYI... to get you the appropriate sp_help, I went ahead and stuck the results of my select statement into a temp table, so the sp_help results for table 1 was pulled from a temp table.
In essence the file represents this: Insert Into DestTable Select T1.*, @changeDate As [Record_Change_Date] Into ##tempTable From SourceTable
so sp_help for 3 tables temp, source, and dest tables.
|
|
|
|
|
SSCrazy Eights
        
Group: General Forum Members
Last Login: Yesterday @ 1:17 PM
Points: 8,641,
Visits: 8,273
|
|
|
|
|
|
SSC Rookie
      
Group: General Forum Members
Last Login: Monday, May 20, 2013 4:33 AM
Points: 25,
Visits: 112
|
|
Hi David,
You have an incorrect query thats the reason why I asked for sp_help tables.
1. When you do an insert with record_change_date the end then it means the last column is record_change_date 2. But record_change_date is your 128th column and not the last 3. Hence you will have to specify the query like this
Insert Into [TableB] Select column1,column2...column 127th,getdate(),column128th...column146th From [tableA] And name NOT IN (Select name From TableA)
Regards Satyen
|
|
|
|
|
Grasshopper
      
Group: General Forum Members
Last Login: Monday, April 22, 2013 12:46 PM
Points: 14,
Visits: 61
|
|
Sean Lange (11/5/2012)
Looks like all you need is: Insert Into [Table1] Select *, GETDATE() From [Table2] Where Record_ID NOT IN (Select Record_ID From Table1)
You could also make your Record_Change_date not null with a default of getdate().
I dont think I can do this, how would SQL know where "GETDATE()" goes? I have to alias it so it can match it up to the destination schema.
I could use getdate(), I've just always followed a practice of putting into a variable so I can use the same date throughout my process.
|
|
|
|
|
Grasshopper
      
Group: General Forum Members
Last Login: Monday, April 22, 2013 12:46 PM
Points: 14,
Visits: 61
|
|
menon.satyen (11/5/2012) Hi David,
You have an incorrect query thats the reason why I asked for sp_help tables.
1. When you do an insert with record_change_date the end then it means the last column is record_change_date 2. But record_change_date is your 128th column and not the last 3. Hence you will have to specify the query like this
Insert Into [TableB] Select column1,column2...column 127th,getdate(),column128th...column146th From [tableA] And name NOT IN (Select name From TableA)
Regards Satyen
I really hope that's not true, I figured as long as the schema's match it would work (I even thought I tried this before). If i have to list the columns it's just not going to work. That would kill the ability to dynamically update the schemas.
I'll take what you say into account and realize it just might not be possible. Thanks
|
|
|
|