在预期条件的上下文中在“ tblProje”附近指定的非布尔类型的表达式

时间:2018-10-10 19:53:09

标签: tsql

在ssms中运行此sql语句时,我收到此错误消息:

在需要条件的上下文中在“ tblProje”附近指定的非布尔类型的表达式

这是语句本身:

PRINT 'Updating FileSetId data from Table Project to Table tblProject'
DECLARE @SQL NVARCHAR(100)
SET @SQL = 'UPDATE  tblProject set tblProject.ProjectFileSetId = Project.FileSetId
FROM Project
WHERE tblProject.AccountingProject = Project.Project_Id'
IF EXISTS(select * from INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME = 'Project' AND COLUMN_NAME = 'FileSetId')
BEGIN
execute sp_executesql @SQL
END
GO

我正在尝试使该语句能够尽可能多地运行。在尝试从列进行更新之前,基本上先进行检查以确保该列存在。我无法确定此错误来自何处

1 个答案:

答案 0 :(得分:0)

如果您查询并查看长度,则会发现一个问题:

DECLARE @SQL NVARCHAR(100)
SET @SQL = 'UPDATE  tblProject set tblProject.ProjectFileSetId = Project.FileSetId
FROM Project
WHERE tblProject.AccountingProject = Project.Project_Id'

select len(@sql)

select len('UPDATE  tblProject set tblProject.ProjectFileSetId = Project.FileSetId
FROM Project
WHERE tblProject.AccountingProject = Project.Project_Id')

您的变量赋值最后会截断41个字符,因此在运行sp_executesql时执行的SQL无效。将变量更改为NVARCHAR(4000)NVARCHAR(MAX)之类的变量,它将起作用。

PRINT 'Updating FileSetId data from Table Project to Table tblProject'
DECLARE @SQL NVARCHAR(MAX)
SET @SQL = 'UPDATE  tblProject set tblProject.ProjectFileSetId = Project.FileSetId
FROM Project
WHERE tblProject.AccountingProject = Project.Project_Id'
IF EXISTS(select * from INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME = 'Project' AND COLUMN_NAME = 'FileSetId')
BEGIN
execute sp_executesql @SQL
END
GO