Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase

Script to list columnnames Expand / Collapse
Author
Message
Posted Thursday, January 10, 2013 2:43 AM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Thursday, February 07, 2013 4:45 AM
Points: 3, Visits: 6
Hi

I often transfer data from one db to another (most 2008 but also 2005 sometimes), i have to generate scripts every time, cos the db is very dynamic, so i havew been using:

declare @SourceDB varchar(11) 
declare @sql varchar(128)
select @SourceDB = 'zzzzzzzz'
create table #tables(name varchar(128))

select @sql = 'insert #tables select TABLE_NAME from ' + @SourceDB + '.INFORMATION_SCHEMA.TABLES where TABLE_TYPE = ''BASE TABLE'''
exec (@sql)

create table #SpaceUsed (name varchar(128), rows varchar(11), reserved varchar(18), data varchar(18), index_size varchar(18), unused varchar(18))
declare @name varchar(128)
select @name = ''
while exists (select * from #tables where name > @name)
begin
select @name = min(name) from #tables where name > @name
select @sql = 'exec ' + @SourceDB + '..sp_executesql N''insert #SpaceUsed exec sp_spaceused ' + @name + ''''
exec (@sql)
end
--select 'Select * From ' + name + char(10)+char(13) from #SpaceUsed where rows > 0
select 'insert into XXX.dbo.' + name + char(10)+ 'Select * From YYY.dbo.' + name + char(10)+char(13) from #SpaceUsed where rows > 0
drop table #tables
drop table #SpaceUsed


But that is not goog enough, as i need to get the column names instead of * and i shall not have an identity column out if one exists, so is it possible to modify the above so i get

insert into table (col1,col2...)
select col1,col2...

instead of insert into table
select * from table

Thanks in advance
Betina

Post #1405290
Posted Thursday, January 10, 2013 4:46 AM


SSCertifiable

SSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiable

Group: General Forum Members
Last Login: Today @ 11:33 AM
Points: 6,713, Visits: 11,748
How about something like this:

-- change to Text Results (press Ctrl+T) before running

-- change this to whichever database your selecting from
USE [YourSourceDatabase];

-- change this to the destination database name
DECLARE @targetDatabase SYSNAME = N'SomeDatabase';

SET NOCOUNT ON;

WITH cte
AS (
SELECT OBJECT_SCHEMA_NAME(p.object_id) AS ObjectSchemaName,
OBJECT_NAME(p.object_id) AS ObjectName,
p.object_id,
STUFF((
SELECT DISTINCT
N',' + QUOTENAME(c.name)
FROM sys.columns c
WHERE p.object_id = c.object_id
FOR
XML PATH('')
), 1, 1, '') AS ColumnList,
SUM(p.rows) AS rows
FROM sys.partitions p
JOIN sys.tables t ON p.object_id = t.object_id
WHERE p.index_id IN (0, 1)
AND t.type_desc = 'USER_TABLE'
AND t.is_ms_shipped = 0
GROUP BY p.object_id
HAVING SUM(p.rows) > 0
)
SELECT N'INSERT INTO ' + QUOTENAME(@targetDatabase) + N'.' + QUOTENAME(ObjectSchemaName) + N'.' + QUOTENAME(ObjectName) + N'(' + ColumnList + N')' + CHAR(13)
+ CHAR(10) + CHAR(9)+ 'SELECT ' + ColumnList + N' FROM ' + QUOTENAME(DB_NAME()) + N'.' + QUOTENAME(ObjectSchemaName) + N'.' + QUOTENAME(ObjectName)
+ ';' + CHAR(13) + CHAR(10)
FROM cte
ORDER BY ObjectSchemaName,
ObjectName;



__________________________________________________________________________________________________
There are no special teachers of virtue, because virtue is taught by the whole community. --Plato

Believe you can and you're halfway there. --Theodore Roosevelt

Everything Should Be Made as Simple as Possible, But Not Simpler --Albert Einstein

The significant problems we face cannot be solved at the same level of thinking we were at when we created them. --Albert Einstein

1 apple is not exactly 1/8 of 8 apples. Because there are no absolutely identical apples. --Giordy
Post #1405345
Posted Friday, January 11, 2013 3:12 AM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Thursday, February 07, 2013 4:45 AM
Points: 3, Visits: 6
Thank you very much, that brings me a lot closer to my goal

I will try to see if i can get the identity columns out.

But I just get 128 chars at each line, so i'll see if thats an option i can mingle with in the management studio.
Post #1405853
Posted Friday, January 11, 2013 4:45 AM


SSCrazy

SSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazy

Group: General Forum Members
Last Login: Tuesday, March 26, 2013 8:41 AM
Points: 2,562, Visits: 3,451
beyan (1/11/2013)
I will try to see if i can get the identity columns out.
for indentity columns , check the column is_identity in sys.columns table , its a bit wise column


-------Bhuvnesh----------
While 1 = 1 (Learning SQL....)
Click to get fast response of your post
Post #1405905
Posted Friday, January 11, 2013 4:48 AM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Thursday, February 07, 2013 4:45 AM
Points: 3, Visits: 6
I have added

 AND c.is_identity = 0


to the WHERE clause in the Stuff function and that did the trick.

Thanks for the help all
Post #1405907
Posted Friday, January 11, 2013 4:53 AM


SSCertifiable

SSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiable

Group: General Forum Members
Last Login: Today @ 11:33 AM
Points: 6,713, Visits: 11,748
You're quite welcome.

__________________________________________________________________________________________________
There are no special teachers of virtue, because virtue is taught by the whole community. --Plato

Believe you can and you're halfway there. --Theodore Roosevelt

Everything Should Be Made as Simple as Possible, But Not Simpler --Albert Einstein

The significant problems we face cannot be solved at the same level of thinking we were at when we created them. --Albert Einstein

1 apple is not exactly 1/8 of 8 apples. Because there are no absolutely identical apples. --Giordy
Post #1405911
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse