[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

microsoft.public.sqlserver.programming

generating sql script by code

Roy Goldhammer

3/19/2007 11:07:00 AM

Hello there

I use sql server 2000 SP4.

Is there a way on query anlyzer to make the action of generating sql script
so i can get some of the script for me?

i need it to get script of creating references in order to build store
procedure that removes all the references and recreate them again.

How can i get this kind of script dinamicly according to the databae i'm
working with


5 Answers

Uri Dimant

3/19/2007 11:18:00 AM

0

Roy
On query analyzer? Can you be more specific.
http://dimantdatabasesolutions.blogspot.com/2007/03/script-ob...




"Roy Goldhammer" <roy@hotmail.com> wrote in message
news:%23XX%23DahaHHA.4616@TK2MSFTNGP03.phx.gbl...
> Hello there
>
> I use sql server 2000 SP4.
>
> Is there a way on query anlyzer to make the action of generating sql
> script so i can get some of the script for me?
>
> i need it to get script of creating references in order to build store
> procedure that removes all the references and recreate them again.
>
> How can i get this kind of script dinamicly according to the databae i'm
> working with
>


Roy Goldhammer

3/19/2007 11:58:00 AM

0

Yes Yri

This is what i want to Achieve.

I need to import data to an emply database from another database while on
the empty database there are relationships.

There are 2 ways i'm thinking doing that:

1. a. remove all the relationships from the database
b. run the DTS
c Restore the relationships as they were before

2. build special query that read all the references in the database and
bring back list of the tables arrange according to the references. so that
if the DTS will run according to this list it can import the data from
another database with no problem

It seems that the first way is the simple one. For that i would like to get
the script of the creating the references in the database so i can keep him
before i remove all the references and at the end of the process i can easly
run it and by that to restore the references

"Uri Dimant" <urid@iscar.co.il> wrote in message
news:%23cshthhaHHA.2448@TK2MSFTNGP02.phx.gbl...
> Roy
> On query analyzer? Can you be more specific.
> http://dimantdatabasesolutions.blogspot.com/2007/03/script-ob...
>
>
>
>
> "Roy Goldhammer" <roy@hotmail.com> wrote in message
> news:%23XX%23DahaHHA.4616@TK2MSFTNGP03.phx.gbl...
>> Hello there
>>
>> I use sql server 2000 SP4.
>>
>> Is there a way on query anlyzer to make the action of generating sql
>> script so i can get some of the script for me?
>>
>> i need it to get script of creating references in order to build store
>> procedure that removes all the references and recreate them again.
>>
>> How can i get this kind of script dinamicly according to the databae i'm
>> working with
>>
>
>


Uri Dimant

3/19/2007 12:16:00 PM

0

Roy
Yes , I'd go with DTS
Also take a look at
http://vyaskn.tripod.com/code/generate_i...






"Roy Goldhammer" <roy@hotmail.com> wrote in message
news:%23P5rj2haHHA.1216@TK2MSFTNGP03.phx.gbl...
> Yes Yri
>
> This is what i want to Achieve.
>
> I need to import data to an emply database from another database while on
> the empty database there are relationships.
>
> There are 2 ways i'm thinking doing that:
>
> 1. a. remove all the relationships from the database
> b. run the DTS
> c Restore the relationships as they were before
>
> 2. build special query that read all the references in the database and
> bring back list of the tables arrange according to the references. so that
> if the DTS will run according to this list it can import the data from
> another database with no problem
>
> It seems that the first way is the simple one. For that i would like to
> get the script of the creating the references in the database so i can
> keep him before i remove all the references and at the end of the process
> i can easly run it and by that to restore the references
>
> "Uri Dimant" <urid@iscar.co.il> wrote in message
> news:%23cshthhaHHA.2448@TK2MSFTNGP02.phx.gbl...
>> Roy
>> On query analyzer? Can you be more specific.
>> http://dimantdatabasesolutions.blogspot.com/2007/03/script-ob...
>>
>>
>>
>>
>> "Roy Goldhammer" <roy@hotmail.com> wrote in message
>> news:%23XX%23DahaHHA.4616@TK2MSFTNGP03.phx.gbl...
>>> Hello there
>>>
>>> I use sql server 2000 SP4.
>>>
>>> Is there a way on query anlyzer to make the action of generating sql
>>> script so i can get some of the script for me?
>>>
>>> i need it to get script of creating references in order to build store
>>> procedure that removes all the references and recreate them again.
>>>
>>> How can i get this kind of script dinamicly according to the databae i'm
>>> working with
>>>
>>
>>
>
>


Razvan Socol

3/19/2007 1:14:00 PM

0

Hello, Roy

If you are concerned only about foreign key constraints (so that you
can insert rows in an empty database, with an arbitrary order of
tables), you can disable the constraints and re-enable them after all
inserts are done, by using something like this:

DECLARE tables CURSOR LOCAL FOR
SELECT name AS TableName, USER_NAME(uid) AS Owner
FROM sysobjects WHERE xtype='U' AND OBJECTPROPERTY(id,'IsMSShipped')=0

DECLARE @TableName sysname, @Owner sysname, @SQL nvarchar(4000)
OPEN tables
WHILE 1=1 BEGIN
FETCH NEXT FROM tables INTO @TableName, @Owner
IF @@FETCH_STATUS<>0 BREAK

SET @SQL='ALTER TABLE '
+QUOTENAME(@Owner)+'.'+QUOTENAME(@TableName)
+'NOCHECK CONSTRAINT ALL'

EXEC (@SQL)
END

CLOSE tables
DEALLOCATE tables


[... insert data in all tables ...]


DECLARE tables CURSOR LOCAL FOR
SELECT name AS TableName, USER_NAME(uid) AS Owner
FROM sysobjects WHERE xtype='U' AND OBJECTPROPERTY(id,'IsMSShipped')=0

DECLARE @TableName sysname, @Owner sysname, @SQL nvarchar(4000)
OPEN tables
WHILE 1=1 BEGIN
FETCH NEXT FROM tables INTO @TableName, @Owner
IF @@FETCH_STATUS<>0 BREAK

SET @SQL='ALTER TABLE '
+QUOTENAME(@Owner)+'.'+QUOTENAME(@TableName)
+'WITH CHECK CHECK CONSTRAINT ALL'

EXEC (@SQL)
END

CLOSE tables
DEALLOCATE tables


This will enable all constraints, even if they were disabled in the
first place. So if you want to keep some constraints disabled, you
should change the script accordingly.

Razvan

Uri Dimant

3/19/2007 1:24:00 PM

0


Also ,take a look at this stored procedure

>need it to get script of creating references in order to build store
>procedure that removes all the references and recreate them again.

CREATE PROCEDURE ScriptTableConstraints
@TableName VARCHAR(100)
AS
BEGIN
SET NOCOUNT ON

SELECT 'ALTER TABLE [' + TC.TABLE_SCHEMA + '].[' + TC.TABLE_NAME + ']' +
CHAR(13)+CHAR(10)+
'DROP CONSTRAINT [' + TC.CONSTRAINT_NAME + ']' +
CHAR(13)+CHAR(10)+CHAR(13)+CHAR(10)
AS [-- Drop Constraints]
FROM INFORMATION_SCHEMA.TABLE_CONSTRAINTS TC
LEFT JOIN INFORMATION_SCHEMA.REFERENTIAL_CONSTRAINTS RC ON
RC.CONSTRAINT_NAME = TC.CONSTRAINT_NAME
LEFT JOIN INFORMATION_SCHEMA.TABLE_CONSTRAINTS FC ON FC.CONSTRAINT_NAME =
RC.UNIQUE_CONSTRAINT_NAME
WHERE (TC.CONSTRAINT_TYPE IN ('PRIMARY KEY', 'UNIQUE') AND TC.TABLE_NAME =
@TableName)
OR (TC.CONSTRAINT_TYPE = 'FOREIGN KEY' AND FC.TABLE_NAME = @TableName)
ORDER BY FC.TABLE_NAME DESC,
INDEXPROPERTY(OBJECT_ID(TC.TABLE_NAME), TC.CONSTRAINT_NAME, 'IsClustered')
ASC


SELECT 'ALTER TABLE [' + TC.TABLE_SCHEMA + '].[' + TC.TABLE_NAME + ']' +
CHAR(13)+CHAR(10)+
'ADD CONSTRAINT [' + TC.CONSTRAINT_NAME + ']' + CHAR(13)+CHAR(10)+
' ' + TC.CONSTRAINT_TYPE +
CASE INDEXPROPERTY(OBJECT_ID(TC.TABLE_NAME), TC.CONSTRAINT_NAME,
'IsClustered')
WHEN 1 THEN ' CLUSTERED'
ELSE ' NONCLUSTERED'
END + CHAR(13)+CHAR(10)+
' (' +
MAX(CASE KCU.ORDINAL_POSITION WHEN 1 THEN '[' + KCU.COLUMN_NAME + ']'
ELSE '' END) +
MAX(CASE KCU.ORDINAL_POSITION WHEN 2 THEN ', [' + KCU.COLUMN_NAME + ']'
ELSE '' END) +
MAX(CASE KCU.ORDINAL_POSITION WHEN 3 THEN ', [' + KCU.COLUMN_NAME + ']'
ELSE '' END) +
MAX(CASE KCU.ORDINAL_POSITION WHEN 4 THEN ', [' + KCU.COLUMN_NAME + ']'
ELSE '' END) +
MAX(CASE KCU.ORDINAL_POSITION WHEN 5 THEN ', [' + KCU.COLUMN_NAME + ']'
ELSE '' END) +
MAX(CASE KCU.ORDINAL_POSITION WHEN 6 THEN ', [' + KCU.COLUMN_NAME + ']'
ELSE '' END) +
MAX(CASE KCU.ORDINAL_POSITION WHEN 7 THEN ', [' + KCU.COLUMN_NAME + ']'
ELSE '' END) +
MAX(CASE KCU.ORDINAL_POSITION WHEN 8 THEN ', [' + KCU.COLUMN_NAME + ']'
ELSE '' END) +
MAX(CASE KCU.ORDINAL_POSITION WHEN 9 THEN ', [' + KCU.COLUMN_NAME + ']'
ELSE '' END) +
MAX(CASE KCU.ORDINAL_POSITION WHEN 10 THEN ', [' + KCU.COLUMN_NAME + ']'
ELSE '' END) +
MAX(CASE KCU.ORDINAL_POSITION WHEN 11 THEN ', [' + KCU.COLUMN_NAME + ']'
ELSE '' END) +
MAX(CASE KCU.ORDINAL_POSITION WHEN 12 THEN ', [' + KCU.COLUMN_NAME + ']'
ELSE '' END) +
MAX(CASE KCU.ORDINAL_POSITION WHEN 13 THEN ', [' + KCU.COLUMN_NAME + ']'
ELSE '' END) +
MAX(CASE KCU.ORDINAL_POSITION WHEN 14 THEN ', [' + KCU.COLUMN_NAME + ']'
ELSE '' END) +
MAX(CASE KCU.ORDINAL_POSITION WHEN 15 THEN ', [' + KCU.COLUMN_NAME + ']'
ELSE '' END) +
MAX(CASE KCU.ORDINAL_POSITION WHEN 16 THEN ', [' + KCU.COLUMN_NAME + ']'
ELSE '' END) +
')' + CHAR(13)+CHAR(10)+
'WITH FILLFACTOR = ' +
CONVERT(varchar(3), --Replace 0 FILLFACTOR with 100
ISNULL(NULLIF(
INDEXPROPERTY(OBJECT_ID(TC.TABLE_NAME), TC.CONSTRAINT_NAME,
'IndexFillFactor'),
0), 100)
) + CHAR(13)+CHAR(10)+
'ON [' + sfg.groupname + ']' + CHAR(13)+CHAR(10)+CHAR(13)+CHAR(10)
AS [-- Create PK/UNIQUE Constraints]
FROM INFORMATION_SCHEMA.TABLE_CONSTRAINTS TC
JOIN INFORMATION_SCHEMA.KEY_COLUMN_USAGE KCU ON TC.TABLE_NAME =
KCU.TABLE_NAME
AND TC.CONSTRAINT_NAME = KCU.CONSTRAINT_NAME
LEFT JOIN sysindexes s ON s.name = TC.CONSTRAINT_NAME
AND s.id = object_id(TC.TABLE_NAME)
LEFT JOIN sysfilegroups sfg ON sfg.groupid = s.groupid
WHERE TC.CONSTRAINT_TYPE IN ('PRIMARY KEY', 'UNIQUE')
AND TC.TABLE_NAME = @TableName
GROUP BY
TC.TABLE_SCHEMA,
TC.TABLE_NAME,
TC.CONSTRAINT_NAME,
TC.CONSTRAINT_TYPE,
sfg.groupname
ORDER BY INDEXPROPERTY(OBJECT_ID(TC.TABLE_NAME), TC.CONSTRAINT_NAME,
'IsClustered') DESC

SELECT 'ALTER TABLE [' + FC.TABLE_SCHEMA + '].[' + FC.TABLE_NAME + ']' +
CHAR(13)+CHAR(10)+
'ADD CONSTRAINT [' + FC.CONSTRAINT_NAME + '] ' + FC.CONSTRAINT_TYPE +
CHAR(13)+CHAR(10)+
' (' +
MAX(CASE KCU_FK.ORDINAL_POSITION WHEN 1 THEN '[' + KCU_FK.COLUMN_NAME +
']' ELSE '' END) +
MAX(CASE KCU_FK.ORDINAL_POSITION WHEN 2 THEN ', [' + KCU_FK.COLUMN_NAME +
']' ELSE '' END) +
MAX(CASE KCU_FK.ORDINAL_POSITION WHEN 3 THEN ', [' + KCU_FK.COLUMN_NAME +
']' ELSE '' END) +
MAX(CASE KCU_FK.ORDINAL_POSITION WHEN 4 THEN ', [' + KCU_FK.COLUMN_NAME +
']' ELSE '' END) +
MAX(CASE KCU_FK.ORDINAL_POSITION WHEN 5 THEN ', [' + KCU_FK.COLUMN_NAME +
']' ELSE '' END) +
MAX(CASE KCU_FK.ORDINAL_POSITION WHEN 6 THEN ', [' + KCU_FK.COLUMN_NAME +
']' ELSE '' END) +
MAX(CASE KCU_FK.ORDINAL_POSITION WHEN 7 THEN ', [' + KCU_FK.COLUMN_NAME +
']' ELSE '' END) +
MAX(CASE KCU_FK.ORDINAL_POSITION WHEN 8 THEN ', [' + KCU_FK.COLUMN_NAME +
']' ELSE '' END) +
MAX(CASE KCU_FK.ORDINAL_POSITION WHEN 9 THEN ', [' + KCU_FK.COLUMN_NAME +
']' ELSE '' END) +
MAX(CASE KCU_FK.ORDINAL_POSITION WHEN 10 THEN ', [' + KCU_FK.COLUMN_NAME
+ ']' ELSE '' END) +
MAX(CASE KCU_FK.ORDINAL_POSITION WHEN 11 THEN ', [' + KCU_FK.COLUMN_NAME
+ ']' ELSE '' END) +
MAX(CASE KCU_FK.ORDINAL_POSITION WHEN 12 THEN ', [' + KCU_FK.COLUMN_NAME
+ ']' ELSE '' END) +
MAX(CASE KCU_FK.ORDINAL_POSITION WHEN 13 THEN ', [' + KCU_FK.COLUMN_NAME
+ ']' ELSE '' END) +
MAX(CASE KCU_FK.ORDINAL_POSITION WHEN 14 THEN ', [' + KCU_FK.COLUMN_NAME
+ ']' ELSE '' END) +
MAX(CASE KCU_FK.ORDINAL_POSITION WHEN 15 THEN ', [' + KCU_FK.COLUMN_NAME
+ ']' ELSE '' END) +
MAX(CASE KCU_FK.ORDINAL_POSITION WHEN 16 THEN ', [' + KCU_FK.COLUMN_NAME
+ ']' ELSE '' END) +
')' + CHAR(13)+CHAR(10)+
'REFERENCES [' + TC.TABLE_SCHEMA + '].[' + TC.TABLE_NAME + ']' +
CHAR(13)+CHAR(10)+
' (' +
MAX(CASE KCU_PK.ORDINAL_POSITION WHEN 1 THEN '[' + KCU_PK.COLUMN_NAME +
']' ELSE '' END) +
MAX(CASE KCU_PK.ORDINAL_POSITION WHEN 2 THEN ', [' + KCU_PK.COLUMN_NAME +
']' ELSE '' END) +
MAX(CASE KCU_PK.ORDINAL_POSITION WHEN 3 THEN ', [' + KCU_PK.COLUMN_NAME +
']' ELSE '' END) +
MAX(CASE KCU_PK.ORDINAL_POSITION WHEN 4 THEN ', [' + KCU_PK.COLUMN_NAME +
']' ELSE '' END) +
MAX(CASE KCU_PK.ORDINAL_POSITION WHEN 5 THEN ', [' + KCU_PK.COLUMN_NAME +
']' ELSE '' END) +
MAX(CASE KCU_PK.ORDINAL_POSITION WHEN 6 THEN ', [' + KCU_PK.COLUMN_NAME +
']' ELSE '' END) +
MAX(CASE KCU_PK.ORDINAL_POSITION WHEN 7 THEN ', [' + KCU_PK.COLUMN_NAME +
']' ELSE '' END) +
MAX(CASE KCU_PK.ORDINAL_POSITION WHEN 8 THEN ', [' + KCU_PK.COLUMN_NAME +
']' ELSE '' END) +
MAX(CASE KCU_PK.ORDINAL_POSITION WHEN 9 THEN ', [' + KCU_PK.COLUMN_NAME +
']' ELSE '' END) +
MAX(CASE KCU_PK.ORDINAL_POSITION WHEN 10 THEN ', [' + KCU_PK.COLUMN_NAME
+ ']' ELSE '' END) +
MAX(CASE KCU_PK.ORDINAL_POSITION WHEN 11 THEN ', [' + KCU_PK.COLUMN_NAME
+ ']' ELSE '' END) +
MAX(CASE KCU_PK.ORDINAL_POSITION WHEN 12 THEN ', [' + KCU_PK.COLUMN_NAME
+ ']' ELSE '' END) +
MAX(CASE KCU_PK.ORDINAL_POSITION WHEN 13 THEN ', [' + KCU_PK.COLUMN_NAME
+ ']' ELSE '' END) +
MAX(CASE KCU_PK.ORDINAL_POSITION WHEN 14 THEN ', [' + KCU_PK.COLUMN_NAME
+ ']' ELSE '' END) +
MAX(CASE KCU_PK.ORDINAL_POSITION WHEN 15 THEN ', [' + KCU_PK.COLUMN_NAME
+ ']' ELSE '' END) +
MAX(CASE KCU_PK.ORDINAL_POSITION WHEN 16 THEN ', [' + KCU_PK.COLUMN_NAME
+ ']' ELSE '' END) +
')' + CHAR(13)+CHAR(10)+CHAR(13)+CHAR(10)
AS [-- Create FK Constraints]
FROM INFORMATION_SCHEMA.TABLE_CONSTRAINTS TC
JOIN INFORMATION_SCHEMA.KEY_COLUMN_USAGE KCU_PK ON TC.TABLE_NAME =
KCU_PK.TABLE_NAME
AND TC.CONSTRAINT_NAME = KCU_PK.CONSTRAINT_NAME
JOIN INFORMATION_SCHEMA.REFERENTIAL_CONSTRAINTS RC ON
RC.UNIQUE_CONSTRAINT_NAME = TC.CONSTRAINT_NAME
JOIN INFORMATION_SCHEMA.TABLE_CONSTRAINTS FC ON FC.CONSTRAINT_NAME =
RC.CONSTRAINT_NAME
JOIN INFORMATION_SCHEMA.KEY_COLUMN_USAGE KCU_FK ON FC.TABLE_NAME =
KCU_FK.TABLE_NAME
AND FC.CONSTRAINT_NAME = KCU_FK.CONSTRAINT_NAME
WHERE TC.CONSTRAINT_TYPE IN ('PRIMARY KEY', 'UNIQUE')
AND TC.TABLE_NAME = @TableName
GROUP BY
FC.TABLE_SCHEMA,
FC.TABLE_NAME,
FC.CONSTRAINT_NAME,
FC.CONSTRAINT_TYPE,
TC.TABLE_SCHEMA,
TC.TABLE_NAME
END


"Uri Dimant" <urid@iscar.co.il> wrote in message
news:uU1GFCiaHHA.4872@TK2MSFTNGP03.phx.gbl...
> Roy
> Yes , I'd go with DTS
> Also take a look at
> http://vyaskn.tripod.com/code/generate_i...
>
>
>
>
>
>
> "Roy Goldhammer" <roy@hotmail.com> wrote in message
> news:%23P5rj2haHHA.1216@TK2MSFTNGP03.phx.gbl...
>> Yes Yri
>>
>> This is what i want to Achieve.
>>
>> I need to import data to an emply database from another database while on
>> the empty database there are relationships.
>>
>> There are 2 ways i'm thinking doing that:
>>
>> 1. a. remove all the relationships from the database
>> b. run the DTS
>> c Restore the relationships as they were before
>>
>> 2. build special query that read all the references in the database and
>> bring back list of the tables arrange according to the references. so
>> that if the DTS will run according to this list it can import the data
>> from another database with no problem
>>
>> It seems that the first way is the simple one. For that i would like to
>> get the script of the creating the references in the database so i can
>> keep him before i remove all the references and at the end of the process
>> i can easly run it and by that to restore the references
>>
>> "Uri Dimant" <urid@iscar.co.il> wrote in message
>> news:%23cshthhaHHA.2448@TK2MSFTNGP02.phx.gbl...
>>> Roy
>>> On query analyzer? Can you be more specific.
>>> http://dimantdatabasesolutions.blogspot.com/2007/03/script-ob...
>>>
>>>
>>>
>>>
>>> "Roy Goldhammer" <roy@hotmail.com> wrote in message
>>> news:%23XX%23DahaHHA.4616@TK2MSFTNGP03.phx.gbl...
>>>> Hello there
>>>>
>>>> I use sql server 2000 SP4.
>>>>
>>>> Is there a way on query anlyzer to make the action of generating sql
>>>> script so i can get some of the script for me?
>>>>
>>>> i need it to get script of creating references in order to build store
>>>> procedure that removes all the references and recreate them again.
>>>>
>>>> How can i get this kind of script dinamicly according to the databae
>>>> i'm working with
>>>>
>>>
>>>
>>
>>
>
>