Halloj... ska köra ett fett sql-script som uppdaterar en äldre version av en db till en nyare. vill ha så att om en tabell i databasen finns så ska den inte skapas... Annars så ska typ create table och allt tjafs köras...
Nån som vet hur jag göra detta... På ett smidigt sätt...
Har pulat lite men det vill lixom inte funka...
kör sql-server... (såklart)
if not exists (select 1 from information_schema.tables
where table_name = 'dinTabell'
and schema_name = 'dittSchema' )
begin
create table dintabell ...
...
end
Nu vet jag inte i vilken version som information_schema infördes. Om det inte finns så får man titta i tabellen sysobjects i stället
if not exists (select 1 from sysobjects
where name = 'dinTabell'
and type in ('U','V'))
begin
create table dintabell ...
...
end
------------------ essentitia preter non sans multiplicandum
Schema_namn..... Va e det? U:et och V:et du petar in i type in... är det några konstanter eller nått?
------------------
- Rickard http://cherub.deca-production.net
"You say I'm beautiful. Well, I can't help it! You say I'm empty. We all know I'm full of shit!"
Server: Msg 170, Level 15, State 1, Line 9
Line 9: Incorrect syntax near 'PRIMARY'.
Server: Msg 1779, Level 16, State 1, Line 2
Table 'tblAgents' already has a primary key defined on it.
Server: Msg 1750, Level 16, State 1, Line 2
Could not create constraint. See previous errors.
Server: Msg 156, Level 15, State 1, Line 3
Incorrect syntax near the keyword 'end'.
plockar jag bort begin och end samt if sats så funkar det men inte annars...
if not exists (select 1 from sysobjects where name = 'tblAgents' and type in ('U','V'))
begin
/****** Object: Table [dbo].[tblAgents] Script Date: 2001-04-18 09:28:29 ******/
CREATE TABLE [dbo].[tblAgents] (
[lAgentID] [int] IDENTITY (1, 1) NOT NULL ,
[sIdentifier] [varchar] (20) NOT NULL ,
[sName] [varchar] (50) NOT NULL ,
[lSalesmanID] [int] NOT NULL
) ON [PRIMARY]
GO
ALTER TABLE [dbo].[tblAgents] WITH NOCHECK ADD
CONSTRAINT [PK_tblAgents] PRIMARY KEY NONCLUSTERED
(
[lAgentID]
) ON [PRIMARY] ,
CONSTRAINT [IX_tblAgents] UNIQUE NONCLUSTERED
(
[sIdentifier]
) ON [PRIMARY]
GO
Print 'Create Table :: tblAgents'
end
------------------
- Rickard http://cherub.deca-production.net
"You say I'm beautiful. Well, I can't help it! You say I'm empty. We all know I'm full of shit!"
Hmm, ta bort båda GO och lägg ett efter end istället.
not exists (select 1 from sysobjects where name = 'tblAgents' and type in ('U','V'))
begin
/****** Object: Table [dbo].[tblAgents] Script Date: 2001-04-18 09:28:29 ******/
CREATE TABLE [dbo].[tblAgents] (
[lAgentID] [int] IDENTITY (1, 1) NOT NULL ,
[sIdentifier] [varchar] (20) NOT NULL ,
[sName] [varchar] (50) NOT NULL ,
[lSalesmanID] [int] NOT NULL
) ON [PRIMARY]
ALTER TABLE [dbo].[tblAgents] WITH NOCHECK ADD
CONSTRAINT [PK_tblAgents] PRIMARY KEY NONCLUSTERED
(
[lAgentID]
) ON [PRIMARY] ,
CONSTRAINT [IX_tblAgents] UNIQUE NONCLUSTERED
(
[sIdentifier]
) ON [PRIMARY]
Print 'Create Table :: tblAgents'
end
GO
Hela blocket blir ju ett statement.
Schema är ett sätt att gruppera ihop objekt....
U och V i kolumnen type i sysobjects står för user defined table respektive vy
Varför alla [], det är väl bara primary som är reserverat ord?
------------------ essentitia preter non sans multiplicandum
------------------
- Rickard http://cherub.deca-production.net
"You say I'm beautiful. Well, I can't help it! You say I'm empty. We all know I'm full of shit!"
271 ms totalt · 4 externa anrop · v20260731065814-full.1dc6f849