Generating a Table Creation Script using SQL Query
DECLARE
@TABLE_NAME VARCHAR(100)
DECLARE
@CUR_TABLE_NAME CURSOR
DELETE FROM ISGSQLWT01.MONet.dbo.Table_Schema_Info_Prod
SET
@CUR_TABLE_NAME = CURSOR
FOR
SELECT TABLE_NAME
FROM INFORMATION_SCHEMA.TABLES where table_name in ('TableName1','TableName2')
OPEN
@CUR_TABLE_NAME
FETCH NEXT
FROM
@CUR_TABLE_NAME INTO @TABLE_NAME
WHILE @@FETCH_STATUS
= 0
BEGIN
INSERT INTO ISGSQLWT01.MONet.dbo.Table_Schema_Info_Prod
SELECT
@TABLE_NAME as
'Table Name',
c.name as 'Column Name',
t.Name as 'Data type',
c.[length] as 'Max Length',
c.[prec] ,
isnull(c.scale,0) ,
columnproperty(c.id,c.name,'AllowsNull'),
columnproperty(c.id,c.name,'IsIdentity')
FROM
syscolumns c
, systypes t
WHERE
c.xtype = t.xtype AND
OBJECT_NAME(c.id) = @TABLE_NAME
FETCH NEXT
FROM @CUR_TABLE_NAME INTO
@TABLE_NAME
END
CLOSE
@CUR_TABLE_NAME
DEALLOCATE @CUR_TABLE_NAME
Comments
Post a Comment