---
title: "if exist"
type: "forum-thread"
url: "https://www.webforum.nu/amne/databaser-sql/21426-if-exist"
topic: "Databaser & SQL"
topic_url: "https://www.webforum.nu/amne/databaser-sql"
author: "cherub"
published: "2001-04-19T08:52:00.000Z"
updated: "2001-04-19T12:34:00.000Z"
replies: 5
views: 233
page: 1
pages: 1
language: "sv-SE"
site: "webForum — webforum.nu"
rights: "Upphovsrätten till varje inlägg tillhör dess författare."
attribution: "Citera som: webForum, https://www.webforum.nu/amne/databaser-sql/21426-if-exist"
---

# if exist

## #1 — cherub, 2001-04-19T08:52Z

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)

tacksam för svar...

Permalänk: https://www.webforum.nu/p/21426

## #2 — LarsG, 2001-04-19T10:22Z

```
  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
```

Permalänk: https://www.webforum.nu/p/351950

## #3 — cherub, 2001-04-19T11:39Z

Schema_namn..... Va e det? U:et och V:et du petar in i type in... är det några konstanter eller nått?

Permalänk: https://www.webforum.nu/p/351951

## #4 — cherub, 2001-04-19T11:45Z

Denna kod ger fel...

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
```

Permalänk: https://www.webforum.nu/p/351952

## #5 — LarsG, 2001-04-19T12:07Z

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?

Permalänk: https://www.webforum.nu/p/351953

## #6 — cherub, 2001-04-19T12:34Z

Tackar, tackar.... du har räddat min dag ;-)

verkar funka fint...

Permalänk: https://www.webforum.nu/p/351954

---

Tråden på webben: https://www.webforum.nu/amne/databaser-sql/21426-if-exist
