我正在使用动态 sql。这是我的 sp。我想检查 null。哪种方法是好的。
ALTER procedure [dbo].[proc_insertsample]
@pname varchar(10),
@rdatetime varchar(10),
@fromcountry int,
@fromoperatorid int
as
begin
declare @query nvarchar(4000)
declare @fromcountry2 varchar(10)
declare @fromoperatorid2 varchar(10)
set @fromcountry2 = CAST(@fromcountry as varchar(10))
set @fromcountry2 = isnull(@fromcountry2,0)
set @fromoperatorid2 = cast (@fromoperatorid as varchar(10))
set @fromoperatorid2 = isnull(@fromoperatorid2,0)
set @query = 'insert into sample1 (pname,rdatetime,fromcountry,fromoperatorid) values ('''+@pname+''','''+@rdatetime+''','''+@fromcountry2+''','''+@fromoperatorid2+''')'
print @query
end
或者
ALTER procedure [dbo].[proc_insertsample2]
@pname varchar(10),
@rdatetime varchar(10),
@fromcountry varchar(10),
@fromoperatorid varchar(10)
as
begin
declare @query nvarchar(4000)
set @fromcountry = ISNULL(@fromcountry,'')
set @fromoperatorid = ISNULL(@fromoperatorid,0)
set @query = 'insert into sample1 (pname,rdatetime,fromcountry,fromoperatorid) values ('''+@pname+''','''+@rdatetime+''','''+@fromcountry+''','''+@fromoperatorid+''')'
print @query
end
.我正在修改 sp。以前 para 是 int ,更改为 varchar。我可以为输入 para 设置一个值吗。我不想再次为 isnull 结果创建变量。我直接将 isnull 放入我正在打印和执行的动态字符串中。但是没有用。