Showing posts with label scripts. Show all posts
Showing posts with label scripts. Show all posts

Tuesday, March 27, 2012

Export database as SQL scripts - Primary key

This is what I did to generate the SQL Scipt of my database in EM (SQL
Server 2000):
- Right click the database
- All Tasks
- Generate SQL Sripts
- Options tab, check the 'Script PRIMARY keys, FOREIGN keys, defaults, and
check constraints.
However, the output script does not define the primary key of my tables.Hi Alan
"Alan T" wrote:
> This is what I did to generate the SQL Scipt of my database in EM (SQL
> Server 2000):
> - Right click the database
> - All Tasks
> - Generate SQL Sripts
> - Options tab, check the 'Script PRIMARY keys, FOREIGN keys, defaults, and
> check constraints.
> However, the output script does not define the primary key of my tables.
>
Have you checked at the end of the file, there should be a load of ALTER
TABLE statements that add primary keys and foreign keys. Fo example in the
pubs database the first ALTER TABLE statement I get is
ALTER TABLE [dbo].[authors] ADD
CONSTRAINT [DF__authors__phone__78B3EFCA] DEFAULT ('UNKNOWN') FOR [phone],
CONSTRAINT [UPKCL_auidind] PRIMARY KEY CLUSTERED
(
[au_id]
) ON [PRIMARY] ,
CHECK ([au_id] like '[0-9][0-9][0-9]-[0-9][0-9]-[0-9][0-9][0-9][0-9]'),
CHECK ([zip] like '[0-9][0-9][0-9][0-9][0-9]')
GO
John|||Yes, thanks.
The ALTER statements are at the bottom.
"John Bell" <jbellnewsposts@.hotmail.com> wrote in message
news:EF1CD898-1644-4ADA-BFD4-C184C1A63436@.microsoft.com...
> Hi Alan
> "Alan T" wrote:
>> This is what I did to generate the SQL Scipt of my database in EM (SQL
>> Server 2000):
>> - Right click the database
>> - All Tasks
>> - Generate SQL Sripts
>> - Options tab, check the 'Script PRIMARY keys, FOREIGN keys, defaults,
>> and
>> check constraints.
>> However, the output script does not define the primary key of my tables.
> Have you checked at the end of the file, there should be a load of ALTER
> TABLE statements that add primary keys and foreign keys. Fo example in the
> pubs database the first ALTER TABLE statement I get is
> ALTER TABLE [dbo].[authors] ADD
> CONSTRAINT [DF__authors__phone__78B3EFCA] DEFAULT ('UNKNOWN') FOR [phone],
> CONSTRAINT [UPKCL_auidind] PRIMARY KEY CLUSTERED
> (
> [au_id]
> ) ON [PRIMARY] ,
> CHECK ([au_id] like '[0-9][0-9][0-9]-[0-9][0-9]-[0-9][0-9][0-9][0-9]'),
> CHECK ([zip] like '[0-9][0-9][0-9][0-9][0-9]')
> GO
> John
>

Export database as SQL scripts - Primary key

This is what I did to generate the SQL Scipt of my database in EM (SQL
Server 2000):
- Right click the database
- All Tasks
- Generate SQL Sripts
- Options tab, check the 'Script PRIMARY keys, FOREIGN keys, defaults, and
check constraints.
However, the output script does not define the primary key of my tables.Hi Alan
"Alan T" wrote:

> This is what I did to generate the SQL Scipt of my database in EM (SQL
> Server 2000):
> - Right click the database
> - All Tasks
> - Generate SQL Sripts
> - Options tab, check the 'Script PRIMARY keys, FOREIGN keys, defaults, and
> check constraints.
> However, the output script does not define the primary key of my tables.
>
Have you checked at the end of the file, there should be a load of ALTER
TABLE statements that add primary keys and foreign keys. Fo example in the
pubs database the first ALTER TABLE statement I get is
ALTER TABLE [dbo].[authors] ADD
CONSTRAINT [DF__authors__phone__78B3EFCA] DEFAULT ('UNKNOWN') FOR [p
hone],
CONSTRAINT [UPKCL_auidind] PRIMARY KEY CLUSTERED
(
[au_id]
) ON [PRIMARY] ,
CHECK ([au_id] like '[0-9][0-9][0-9]-[0-9][0-9]-[
;0-9][0-9][0-9][0-9]'),
CHECK ([zip] like '[0-9][0-9][0-9][0-9][0-9]')
GO
John|||Yes, thanks.
The ALTER statements are at the bottom.
"John Bell" <jbellnewsposts@.hotmail.com> wrote in message
news:EF1CD898-1644-4ADA-BFD4-C184C1A63436@.microsoft.com...
> Hi Alan
> "Alan T" wrote:
>
> Have you checked at the end of the file, there should be a load of ALTER
> TABLE statements that add primary keys and foreign keys. Fo example in the
> pubs database the first ALTER TABLE statement I get is
> ALTER TABLE [dbo].[authors] ADD
> CONSTRAINT [DF__authors__phone__78B3EFCA] DEFAULT ('UNKNOWN') FOR [
;phone],
> CONSTRAINT [UPKCL_auidind] PRIMARY KEY CLUSTERED
> (
> [au_id]
> ) ON [PRIMARY] ,
> CHECK ([au_id] like '[0-9][0-9][0-9]-[0-9][0-9]-&#
91;0-9][0-9][0-9][0-9]'),
> CHECK ([zip] like '[0-9][0-9][0-9][0-9][0-9]')
> GO
> John
>

Monday, March 26, 2012

Export data from table as SQL Inserts

Hi,
I have data in an old database I would like to capture for my newsystem but I dont have the original insert scripts. Is there atool (in SQL Server 2000 or thirdparty) that will help me export thedata as SQL inserts?
Thanks
jr.
Use DTS. Are they both SQL Server databases? If so, you canimport data from one database to another. Use the Import Datawizard. If the old database is not SQL Server, export the file toCSV, and the use SQL Server's Import Data wizard to import from the CSVfile.

Sunday, February 26, 2012

EXIT Installation Scripts that have multiple GO statements

Hi All

I'm Scripting various Upgrade scripts that set up roles / users / create databases etc and want to Exit the Script unconditionally if for example the user already exists etc.

I can't Use RETURN because it just jumps to the Code following the next GO statement

There is also some USE [Database] Statements

Is there a way to do this or am I missing something fundamental here ?

GWIs there a way to do this or am I missing something fundamental here ?No.

-PatP|||I think conditional statements are the way to go...

If the username exists, do nothing, else do something...
OR
If the username doesn't exist, do something.|||What if he dumped the monolithic type script and removed the batch separators instead calling a series of individual files in series? Sean posted a script to do this recently and I have a similar one but using sqlcmd.

George - I think the problem is that batch separators are required as otherwise the deferred name resolution causes the script to fail as dependant objects do not exist at compile time. Also, some objects (e.g. functions & procs) can be the only statements in a batch.|||this can be done BUT the script must always be run by either SQLCMD or maybe OSQL. Here is a sampling of the file that you would use;

select count(*)
from sys.databases
go

if exists (select * from sys.tables)
begin
exit
end

go

select count(*)
from sys.databases
go

This will always generate an error in Query Analyzer, but SQLCMD will exit as you desire.

EDIT: Note; when you test this script, you only get the results of the first query. The second query is ignored.|||Folks

Thanks for all your input & Apologies for not getting back to you sooner.

In this instance the new system is still in Dev & I'm releasing full versions into UAT / Testing environments (which comprise 2 new interface databases, Linked Servers & multiple security accounts setup) - hence the monolothic script.

I think I'll have to accept Pat's Answer.

Thanks again

GW