Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Select * into shortcut

Author  Topic 

WJHamel
Aged Yak Warrior

651 Posts

Posted - 2012-03-23 : 10:10:08
Just wondering if there is an easier way to do this:

I have two tables. Table A has "some" columns identical to Table "B". I need to append the data in those like columns into Table "B". Do i need to enumerate those columns by name, one by one, in my Select Into statement or is there something similar to an "If Exists" that can be used to select only those columns that Table A and Table B have in common?

I can provide table schemas if really needed.

thanks

james

sunitabeck
Master Smack Fu Yak Hacker

5155 Posts

Posted - 2012-03-23 : 10:17:17
There is no direct "IF EXISTS" type of option for what you are looking to do. The two alternatives are:

1. This assumes that the omitted columns in TableB are either nullable or have default values.
insert into TableB (col1, col7, col11)
select colA, colK, colC from TableA;


2. Provide default values in the select query

insert into TableB (col1, col2, col3, col4)
select colA, 'abcd',null,ColC from TableA
Go to Top of Page

WJHamel
Aged Yak Warrior

651 Posts

Posted - 2012-03-23 : 10:26:34
Thank you. If my noncommon columns in tableB are all nullable, then i don't need to use option 2, correct?

Go to Top of Page

X002548
Not Just a Number

15586 Posts

Posted - 2012-03-23 : 10:59:00
Or you can write code to do it for you




DECLARE @t1 varchar(255), @t2 varchar(255)
SELECT @t1 = 'myMessage', @t2 = 'User_Message'

SELECT SQL FROM (
SELECT 'INSERT INTO ' + TABLE_NAME + ' ( ' AS SQL
, 1 AS SQL_CODE, 1 AS ORDINAL_POSITION, TABLE_NAME
FROM INFORMATION_SCHEMA.Tables
WHERE TABLE_NAME = @t1
UNION ALL
SELECT ' ' + COLUMN_NAME AS SQL
, 2 AS SQL_CODE, ORDINAL_POSITION, TABLE_NAME
FROM INFORMATION_SCHEMA.Columns co
WHERE TABLE_NAME = @t1
AND EXISTS (SELECT *
FROM INFORMATION_SCHEMA.Columns ci
WHERE TABLE_NAME = @t2
AND ci.COLUMN_NAME = co.COLUMN_NAME)
AND EXISTS (SELECT *
FROM INFORMATION_SCHEMA.Columns ci
WHERE TABLE_NAME = @t1
AND ci.COLUMN_NAME = co.COLUMN_NAME
GROUP BY ci.COLUMN_NAME
HAVING co.ORDINAL_POSITION = MIN(ci.ORDINAL_POSITION))

UNION ALL
SELECT ',' + COLUMN_NAME AS SQL
, 2 AS SQL_CODE, ORDINAL_POSITION, TABLE_NAME
FROM INFORMATION_SCHEMA.Columns co
WHERE TABLE_NAME = @t1
AND EXISTS (SELECT *
FROM INFORMATION_SCHEMA.Columns ci
WHERE TABLE_NAME = @t2
AND ci.COLUMN_NAME = co.COLUMN_NAME)
AND NOT EXISTS (SELECT *
FROM INFORMATION_SCHEMA.Columns ci
WHERE TABLE_NAME = @t1
AND ci.COLUMN_NAME = co.COLUMN_NAME
GROUP BY ci.COLUMN_NAME
HAVING co.ORDINAL_POSITION = MIN(ci.ORDINAL_POSITION))
UNION ALL
SELECT ')' AS SQL
, 3 AS SQL_CODE, 1 AS ORDINAL_POSITION, TABLE_NAME
FROM INFORMATION_SCHEMA.Tables
WHERE TABLE_NAME = @t1
UNION ALL
SELECT 'SELECT ' AS SQL
, 4 AS SQL_CODE, 1 AS ORDINAL_POSITION, TABLE_NAME
FROM INFORMATION_SCHEMA.Tables
WHERE TABLE_NAME = @t2
UNION ALL
SELECT ' ' + COLUMN_NAME AS SQL
, 5 AS SQL_CODE, ORDINAL_POSITION, TABLE_NAME
FROM INFORMATION_SCHEMA.Columns co
WHERE TABLE_NAME = @t2
AND EXISTS (SELECT *
FROM INFORMATION_SCHEMA.Columns ci
WHERE TABLE_NAME = @t1
AND ci.COLUMN_NAME = co.COLUMN_NAME)
AND EXISTS (SELECT *
FROM INFORMATION_SCHEMA.Columns ci
WHERE TABLE_NAME = @t2
AND ci.COLUMN_NAME = co.COLUMN_NAME
GROUP BY ci.COLUMN_NAME
HAVING co.ORDINAL_POSITION = MIN(ci.ORDINAL_POSITION))

UNION ALL
SELECT ',' + COLUMN_NAME AS SQL
, 6 AS SQL_CODE, ORDINAL_POSITION, TABLE_NAME
FROM INFORMATION_SCHEMA.Columns co
WHERE TABLE_NAME = @t2
AND EXISTS (SELECT *
FROM INFORMATION_SCHEMA.Columns ci
WHERE TABLE_NAME = @t1
AND ci.COLUMN_NAME = co.COLUMN_NAME)
AND NOT EXISTS (SELECT *
FROM INFORMATION_SCHEMA.Columns ci
WHERE TABLE_NAME = @t2
AND ci.COLUMN_NAME = co.COLUMN_NAME
GROUP BY ci.COLUMN_NAME
HAVING co.ORDINAL_POSITION = MIN(ci.ORDINAL_POSITION))
UNION ALL
SELECT ' FROM ' + TABLE_NAME AS SQL
, 7 AS SQL_CODE, 1 AS ORDINAL_POSITION, TABLE_NAME
FROM INFORMATION_SCHEMA.Tables
WHERE TABLE_NAME = @t2
) AS XXX




Brett

8-)

Hint: Want your questions answered fast? Follow the direction in this link
http://weblogs.sqlteam.com/brettk/archive/2005/05/25/5276.aspx


Want to help yourself?

http://msdn.microsoft.com/en-us/library/ms130214.aspx

http://weblogs.sqlteam.com/brettk/

http://brettkaiser.blogspot.com/


Go to Top of Page

sunitabeck
Master Smack Fu Yak Hacker

5155 Posts

Posted - 2012-03-23 : 11:02:02
quote:
Originally posted by WJHamel

Thank you. If my noncommon columns in tableB are all nullable, then i don't need to use option 2, correct?

Correct. It is even ok to have non-nullable columns if they have a default.
Go to Top of Page

sunitabeck
Master Smack Fu Yak Hacker

5155 Posts

Posted - 2012-03-23 : 11:02:42
Brett, you are becoming almost like me!! Trying to write the longest and most complex query possible
Go to Top of Page

X002548
Not Just a Number

15586 Posts

Posted - 2012-03-23 : 11:04:32
ummmmm....I've been obfuscating since you were in diapers

Besides....it's very simple...I've saved scripts for EVERY variation to Gen Code



Brett

8-)

Hint: Want your questions answered fast? Follow the direction in this link
http://weblogs.sqlteam.com/brettk/archive/2005/05/25/5276.aspx


Want to help yourself?

http://msdn.microsoft.com/en-us/library/ms130214.aspx

http://weblogs.sqlteam.com/brettk/

http://brettkaiser.blogspot.com/


Go to Top of Page

WJHamel
Aged Yak Warrior

651 Posts

Posted - 2012-03-23 : 11:26:31
overly complex or not, that one's going in my library.
Go to Top of Page

X002548
Not Just a Number

15586 Posts

Posted - 2012-03-23 : 13:26:16
Did you cut and paste it, change the table names and run it?

Brett

8-)

Hint: Want your questions answered fast? Follow the direction in this link
http://weblogs.sqlteam.com/brettk/archive/2005/05/25/5276.aspx


Want to help yourself?

http://msdn.microsoft.com/en-us/library/ms130214.aspx

http://weblogs.sqlteam.com/brettk/

http://brettkaiser.blogspot.com/


Go to Top of Page

WJHamel
Aged Yak Warrior

651 Posts

Posted - 2012-03-27 : 10:59:08
not yet. i've been distracted by a shiny object.
Go to Top of Page
   

- Advertisement -