數據庫中有兩張表,A表主鍵為自動增長的並且是B表的外鍵且允許為空。現在要通過編程向B表中插入數據,可是在程序中是不允許給Int類型賦空值的如果不賦值就默認為0。為了解決這個為題最後考慮用存儲過程的If Else最方便,具體的存儲過程如下
create PROCEDURE [dbo].[P_Form_Control_Info_Add]
@TypeName varchar(20),
@Description varchar(50),
@CtlColSpan int,
@Sort int,
@SourceID int,
@FIEldID int,
@TableID int
AS
if @SourceID = 0
begin
INSERT INTO T_Form_Control_Info (
[TypeName],
[Description],
[CtlColSpan],
[Sort],
[FIEldID],
[TableID]
) VALUES (
@TypeName,
@Description,
@CtlColSpan,
@Sort,
@FIEldID,
@TableID
)
end
else
begin
INSERT INTO T_Form_Control_Info (
[TypeName],
[Description],
[CtlColSpan],
[Sort],
[SourceID],
[FIEldID],
[TableID]
) VALUES (
@TypeName,
@Description,
@CtlColSpan,
@Sort,
@SourceID,
@FIEldID,
@TableID
)
end
return SCOPE_IDENTITY()