我正在使用SQL 2012,我希望所有代码都能同时工作。但是给我这个错误。 我应该如何走。 谢谢。
BEGIN
USE [DLSaglikNet]
CREATE TABLE PASSAPORT_TURU
(
ID int NOT NULL PRIMARY KEY IDENTITY (1,1),
ADI varchar(70) ,
KODU varchar(20)
)
INSERT INTO PASSAPORT_TURU (ADI,KODU)
VALUES
('Passaport Numarası','1'),
('Yabancı Hasta T.C. Kimliği','2'),
('Lütfen Seçim Yapınız.','3')
Alter table HASTA Add PASSAPORTTURU int CONSTRAINT FK_HASTA_PASSAPORT_TURU foreign key (PASSAPORTTURU) REFERENCES PASSAPORT_TURU(ID)
Update HASTA set PASSAPORTTURU = 1 where PASSAPORT IS NOT NULL and TCKIMLIK IS NULL and PASSAPORTTURU IS NULL
Update HASTA set PASSAPORTTURU = 3 where PASSAPORT IS NULL and TCKIMLIK IS NOT NULL
END;
GO
答案 0 :(得分:4)
在执行DDL语句时,需要在其后使用GO
关键字,这意味着将执行批处理和脚本。
这些命令可用于简化批处理和脚本的可读性和执行。
确保
Alter table HASTA Add PASSAPORTTURU int CONSTRAINT FK_HASTA_PASSAPORT_TURU foreign key (PASSAPORTTURU) REFERENCES PASSAPORT_TURU(ID)
在插入数据之前执行。
BEGIN
USE [DLSaglikNet]
CREATE TABLE PASSAPORT_TURU ( ID int NOT NULL PRIMARY KEY IDENTITY (1,1), ADI varchar(70) , KODU varchar(20) )
INSERT INTO PASSAPORT_TURU (ADI,KODU) VALUES ('Passaport Numarası','1'), ('Yabancı Hasta T.C. Kimliği','2'), ('Lütfen Seçim Yapınız.','3')
Alter table HASTA Add PASSAPORTTURU int CONSTRAINT FK_HASTA_PASSAPORT_TURU foreign key (PASSAPORTTURU) REFERENCES PASSAPORT_TURU(ID)
GO -- <-- ADD GO HERE.
Update HASTA set PASSAPORTTURU = 1 where PASSAPORT IS NOT NULL and TCKIMLIK IS NULL and PASSAPORTTURU IS NULL
Update HASTA set PASSAPORTTURU = 3 where PASSAPORT IS NULL and TCKIMLIK IS NOT NULL
END;.
答案 1 :(得分:1)
您需要先检查您的alter是否存在,然后添加到代码末尾:
if not exist(select PASSAPORTTURU from HASTA )
Alter table HASTA Add PASSAPORTTURU int CONSTRAINT FK_HASTA_PASSAPORT_TURU foreign key (PASSAPORTTURU) REFERENCES PASSAPORT_TURU(ID)
go --here adding go
Update HASTA set PASSAPORTTURU = 1 where PASSAPORT IS NOT NULL and TCKIMLIK IS NULL and PASSAPORTTURU IS NULL
go
Update HASTA set PASSAPORTTURU = 3 where PASSAPORT IS NULL and TCKIMLIK IS NOT NULL
答案 2 :(得分:0)
在更新语句行上方使用Go关键字。
Go
Update statements
这将确保alter语句在尝试更新表之前已经发生。