sql中使用变量出错
目的:将数据库名设为变量,方便将来修改。如下T/SQL出错:declare@dbXFZvarchar(30)declare@SQLStringvarchar(500)se...
目的:将数据库名设为变量,方便将来修改。如下T/SQL出错:
declare @dbXFZ varchar(30)
declare@SQLString varchar(500)
select @dbXFZ="PantoSckoolXFZ.dbo."
select @SQLString="insert into XSK_Grade(NJDM,NJMC,RXNF,mark)SELECT NJDM,NJMC,RXNF,mark FROM "+@dbXFZ+"jx_Grade where NJDM not in (select NJDM from XSK_Grade)"
exec sp_executesql @SQLString
若不使用变量,则可以正常执行:
insert into
XSK_Grade
(NJDM,NJMC,RXNF,mark)
SELECT NJDM,NJMC,RXNF,mark
FROM pantoschoolxfz.dbo.jx_Grade
where
NJDM not in (select NJDM from XSK_grade)
注:此SQL代码是用来做数据库定时作业的。请高人指教。 展开
declare @dbXFZ varchar(30)
declare@SQLString varchar(500)
select @dbXFZ="PantoSckoolXFZ.dbo."
select @SQLString="insert into XSK_Grade(NJDM,NJMC,RXNF,mark)SELECT NJDM,NJMC,RXNF,mark FROM "+@dbXFZ+"jx_Grade where NJDM not in (select NJDM from XSK_Grade)"
exec sp_executesql @SQLString
若不使用变量,则可以正常执行:
insert into
XSK_Grade
(NJDM,NJMC,RXNF,mark)
SELECT NJDM,NJMC,RXNF,mark
FROM pantoschoolxfz.dbo.jx_Grade
where
NJDM not in (select NJDM from XSK_grade)
注:此SQL代码是用来做数据库定时作业的。请高人指教。 展开
4个回答
展开全部
declare @dbXFZ varchar(30),@SQLString varchar(500)
select @dbXFZ='PantoSckoolXFZ.dbo.'
select @SQLString='insert into XSK_Grade(NJDM,NJMC,RXNF,mark)SELECT NJDM,NJMC,RXNF,mark FROM '+@dbXFZ+'jx_Grade where NJDM not in (select NJDM from XSK_Grade)'
exec (@SQLString)
select @dbXFZ='PantoSckoolXFZ.dbo.'
select @SQLString='insert into XSK_Grade(NJDM,NJMC,RXNF,mark)SELECT NJDM,NJMC,RXNF,mark FROM '+@dbXFZ+'jx_Grade where NJDM not in (select NJDM from XSK_Grade)'
exec (@SQLString)
展开全部
insert into
XSK_Grade
values(NJDM,NJMC,RXNF,mark)
as SELECT NJDM,NJMC,RXNF,mark
FROM pantoschoolxfz.dbo.jx_Grade
where
NJDM not in (select NJDM from XSK_grade)
XSK_Grade
values(NJDM,NJMC,RXNF,mark)
as SELECT NJDM,NJMC,RXNF,mark
FROM pantoschoolxfz.dbo.jx_Grade
where
NJDM not in (select NJDM from XSK_grade)
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
把你的语句中的双引号全部改成单引号就可以了
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
select @SQLString="insert into XSK_Grade(NJDM,NJMC,RXNF,mark)SELECT NJDM,NJMC,RXNF,mark FROM "+@dbXFZ+"jx_Grade where NJDM not in (select NJDM from XSK_Grade)"
这句的双引号都改成单引号
另外
declare@SQLString varchar(500)找个长度最好设大一点,防止sql语句放不下
这句的双引号都改成单引号
另外
declare@SQLString varchar(500)找个长度最好设大一点,防止sql语句放不下
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询