我正在编写此商店程序,但一直收到错误。
我的程序:
USE AllCtuStudentInfo
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE SelectProcedure4
@myVar VARCHAR(8000),
@Schema varchar(50),
@ID int
--@ix INT = 1
AS
--WHILE @ix < 1000
BEGIN
set @myVar = 'SELECT '+@Schema+'.Student.Name, '+@Schema+'.Student.Surname, '+@Schema+'.Course.Name AS Course,'+@Schema+'.LearningUnit.Name AS [Learning Unit], '+@Schema+'.Formative.[Formative Name], '+@Schema+'.FormativeMark.Mark, '+@Schema+'.Summative.[Summative Name], '+@Schema+'.SummativeMark.Mark AS Mark FROM '+@Schema+'.LearningUnit INNER JOIN '+@Schema+'.Formative INNER JOIN '+@Schema+'.Course INNER JOIN '+@Schema+'.SummativeMark INNER JOIN '+@Schema+'.FormativeMark INNER JOIN '+@Schema+'.Student ON '+@Schema+'.FormativeMark.FK_Student = '+@Schema+'.Student.ID ON '+@Schema+'.SummativeMark.FK_Student = '+@Schema+'.Student.ID ON '+@Schema+'.Course.ID = '+@Schema+'.Student.FK_Course INNER JOIN '+@Schema+'.Summative ON '+@Schema+'.SummativeMark.FK_Summative = '+@Schema+'.Summative.ID ON '+@Schema+'.Formative.ID = '+@Schema+'.FormativeMark.FK_Formative ON '+@Schema+'.LearningUnit.ID = '+@Schema+'.Summative.FK_LU AND '+@Schema+'.LearningUnit.ID = '+@Schema+'.Formative.FK_LU WHERE '+@Schema+'.Student.ID = '''+CAST(@ID AS VARCHAR(10))+''';'
--SET @ix = @ix + 1
EXEC @myVar
END
我如何执行它:
USE AllCtuStudentInfo
EXEC SelectProcedure4 @myVar = '', @Schema = 'Auckland_Park', @ID = 1
错误:
Msg 203,Level 16,State 2,Procedure SelectProcedure4,Line 15
名称'SELECT Auckland_Park.Student.Name, Auckland_Park.Student.Surname,Auckland_Park.Course.Name AS 课程,Auckland_Park.LearningUnit.Name AS [学习单元], Auckland_Park.Formative。[形式名称], Auckland_Park.FormativeMark.Mark,Auckland_Park.Summative。[Summative 名称],Auckland_Park.SummativeMark.Mark AS Mark FROM Auckland_Park.LearningUnit INNER JOIN Auckland_Park.Formative INNER 加入Auckland_Park.Course INNER加入Auckland_Park.SummativeMark INNER 加入Auckland_Park.FormativeMark INNER JOIN Auckland_Park.Student ON Auckland_Park.FormativeMark.FK_Student = Auckland_Park.Student.ID ON Auckland_Park.SummativeMark.FK_Student =奥克兰'无效 标识符
你可以看到它被切断了。我尝试了其他一些方法,但我总是遇到同样的错误,它会在同一个地方被切断。
请帮忙!
答案 0 :(得分:3)
下面是一个使用sp_executesql和参数化查询的示例。
CREATE PROCEDURE dbo.SelectProcedure4
@myVar nvarchar(MAX),
@Schema sysname,
@ID int
AS
DECLARE @SchemaName nvarchar(130) = QUOTENAME(@Schema);
SET @myVar = 'SELECT '+@SchemaName+'.Student.Name, '+@SchemaName+'.Student.Surname, '+@SchemaName+'.Course.Name AS Course,'+@SchemaName+'.LearningUnit.Name AS [Learning Unit], '+@SchemaName+'.Formative.[Formative Name], '+@SchemaName+'.FormativeMark.Mark, '+@SchemaName+'.Summative.[Summative Name], '+@SchemaName+'.SummativeMark.Mark AS Mark FROM '+@SchemaName+'.LearningUnit INNER JOIN '+@SchemaName+'.Formative INNER JOIN '+@SchemaName+'.Course INNER JOIN '+@SchemaName+'.SummativeMark INNER JOIN '+@SchemaName+'.FormativeMark INNER JOIN '+@SchemaName+'.Student ON '+@SchemaName+'.FormativeMark.FK_Student = '+@SchemaName+'.Student.ID ON '+@SchemaName+'.SummativeMark.FK_Student = '+@SchemaName+'.Student.ID ON '+@SchemaName+'.Course.ID = '+@SchemaName+'.Student.FK_Course INNER JOIN '+@SchemaName+'.Summative ON '+@SchemaName+'.SummativeMark.FK_Summative = '+@SchemaName+'.Summative.ID ON '+@SchemaName+'.Formative.ID = '+@SchemaName+'.FormativeMark.FK_Formative ON '+@SchemaName+'.LearningUnit.ID = '+@SchemaName+'.Summative.FK_LU AND '+@SchemaName+'.LearningUnit.ID = '+@SchemaName+'.Formative.FK_LU WHERE '+@SchemaName+'.Student.ID = @ID;'
EXECUTE sp_executesql @myVar, N'@ID int', @ID= @ID;
GO