我知道这对你们大多数人来说都很模糊,但在my first question on stackoverflow之后我决定请求你们帮忙创建一个修改这个表的存储过程(对于通用格式抱歉): / p>
Table1(
ColumnID int not null, --autoincremented
ForeignKey1 int,
ForeignKey2 int,
ForeignKey3 int,
ForeignKey4 int,
Column1 varchar,
Column2 varchar,
Column3 nvarchar,
PRIMARY KEY (ColumnID)
)
我想到的存储过程是这样的:
CREATE PROCEDURE usp_ModifyTable
@statementType varchar, --auxilliary variable used only in procedure
@paramFK1 int,
@paramFK2 int,
@paramFK3 int,
@paramFK4 int,
@paramCol1 varchar, -- has UNIQUE constraint
@paramCol2 varchar, -- has UNIQUE constraint
@paramCol3 nvarchar
AS
BEGIN
IF @statementType = 'Insert'
BEGIN
INSERT INTO Table1(
ForeignKey1,ForeignKey2,ForeignKey3,ForeignKey4,Column1,Column2,Column3)
VALUES(
@paramFK1,@paramFK2,@paramFK3,@paramFK4,@paramCol1,@paramCol2,@paramCol3)
END
IF @statementType = 'Update'
BEGIN
UPDATE Table1
SET ForeignKey1 = @paramFK1, ForeignKey2 = @paramFK2,
ForeignKey3 = @paramFK3,ForeignKey4 = @paramFK4,Column1 = @paramCol1,
Column2 = @paramCol2,Column3 = @paramCol3
WHERE Column1 = @paramCol1 OR Column2 = @paramCol2
END
IF @statementType = 'Delete'
BEGIN
DELETE FROM Table1
WHERE Column1 = @paramCol1 OR Column2 = @paramCol2
END
现在我的问题:
1.我应该为ColumnID
添加参数吗?如果是这样,它会改变程序中的某些内容(比如在插入时请求ID,即使它是标识)?
2.如果我在IF中放置一些参数(如DECLARE @param
)会不会出现复杂情况?