October 14, 2010 at 7:19 am
How to create a Script to insert only columns' name of one table into another table?
October 14, 2010 at 7:23 am
October 14, 2010 at 7:25 am
using my magic hat and mind reading glasses, i read your request two ways differently than mazzz did:
1. that you really meant "create an empty table with the same column names:
2. I need all the column names for a table as a comma delimited list.
--create an empty table with the same column names
SELECT *
INTO NEWTABLENAME
FROM ORIGINALTABLE
WHERE 1=2 --this makes the structure, but no data
--column names for a table as a comma delimited list
SELECT DISTINCT
t.name,
sq.Columns
FROM sys.tables t
JOIN (
SELECT OBJECT_ID,
Columns = STUFF((SELECT ',' + name
FROM sys.columns sc
WHERE sc.object_id = s.object_id
FOR XML PATH('')),1,1,'')
FROM sys.columns s
) sq ON t.object_id = sq.object_id
if my best guess wasn't close, can you explain the needs a little deeper?
Lowell
October 14, 2010 at 8:02 am
Thank you for help. I used code below. It works fine.
select column_name
into NewTable
FROM INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_NAME = 'myTable'
October 14, 2010 at 9:19 am
Looks like you're making things more difficult than necessary Lowell! ;-):w00t:
Wayne
Microsoft Certified Master: SQL Server 2008
Author - SQL Server T-SQL Recipes
October 14, 2010 at 9:50 am
WayneS (10/14/2010)
Looks like you're making things more difficult than necessary Lowell! ;-):w00t:
clearly!
A lot like you, I'm trying to anticipate the requirement and provide a solid example, but obviously i jumped to far into the best guess scanrio there....whoops!
Lowell
Viewing 6 posts - 1 through 5 (of 5 total)
You must be logged in to reply to this topic. Login to reply