我使用的是用户定义的表格类型,如下所示。注意,请我动态生成查询字符串并在结尾处执行字符串。你可能会注意到一些引用
ALTER PROCEDURE MyPROC
@phoneNo nvarchar(30) = null,
@status dbo.StatusViewTableType READONLY
AS
BEGIN
if(@option = 1)
begin
set @sql = 'SELECT c.id,c.name,c.NewIc,c.OldIc,c.Title,c.nationality,c.CreatedDate,c.companyNo,c.NonIc,c.Email
FROM customer c WITH (NOLOCK)
left outer join
dbo.Address with(nolock) on c.id = dbo.Address.CustomerID
left outer join dbo.ContactNo on c.id=dbo.ContactNo.CustomerID
WHERE (c.id > 0) and ('
if @oldIC <>'' and @phoneNo=''
set @sql = @sql + ' (oldic ='''+ @oldIC + ''' )or'
if @newIC <>'' and @phoneNo=''
set @sql = @sql + ' (newIC ='''+ @newIC + ''' )or'
if @companyno <>'' and @phoneNo=''
set @sql = @sql + ' (companyno ='''+ @companyno + ''' )or'
if @nonic <>'' and @phoneNo=''
set @sql = @sql + ' (nonic ='''+ @nonic + ''' )or'
If @phoneNo <>'' and @oldIC = '' and @newIC = '' and @companyno = '' and @nonic =''
begin
set @sql ='SELECT c.id,c.name,c.NewIc,c.OldIc,c.Title,c.nationality,c.CreatedDate,c.companyNo,c.NonIc,c.Email
FROM dbo.Customer WITH (NOLOCK) c INNER JOIN
dbo.ContactNo WITH (NOLOCK) ON c.id = dbo.ContactNo.CustomerID
where ContactNo = '''+ @phoneNo + ''' and dbo.ContactNo.Contactability in (SELECT s.status from ' + [@status] + ' AS s ) '
end
if @phoneNo=''
begin
set @sql = Left(@sql,Len(@sql)-2)
set @sql = @sql + ')'
set @sql = @sql + ' and ( dbo.Address.Contactable in (SELECT s.status from ' + [@status] + ' AS s) or dbo.ContactNo.Contactability in (SELECT s.status from ' + [@status] + ' AS s) )'
end
print @sql
exec (@sql)
end
结束
在我的后端代码中,我传递如下参数:
SqlConnection myConnection = null;
SqlCommand myCommand = null;
SqlDataReader myReader = null;
myConnection = new SqlConnection(connectionString);
myCommand = myConnection.CreateCommand();
myCommand.CommandText = "GETCUSTOMER";
myCommand.CommandType = CommandType.StoredProcedure;
myCommand.CommandTimeout = 0;
SqlParameter parameter;
int l = ids.FirstOrDefault().Length;
if (ids.FirstOrDefault().Length > 0)
{
if (useDataTable)
{
parameter = myCommand.Parameters.AddWithValue("@status", CreateDataTable(ids));
}
else
{
parameter = myCommand.Parameters.AddWithValue("@status", CreateSqlDataRecords(ids));
}
parameter.SqlDbType = SqlDbType.Structured;
parameter.TypeName = "dbo.StatusViewTableType";
}
myCommand.Parameters.Add(new SqlParameter("@phoneNo", SqlDbType.NVarChar));
myCommand.Parameters["@phoneNo"].Value = phoneNo;
我遇到参数@status
的问题。它在存储过程中无法识别。
这是一个例外:
必须声明标量变量“@status”。
以下是我的StatusViewTAbleType
。
CREATE TYPE [dbo].[StatusViewTableType] AS TABLE(
[Status] [int] NOT NULL
)
GO
任何帮助都将不胜感激。
答案 0 :(得分:1)
如果在完全限定的对象名称中使用表值参数,则必须将其括在括号中,即代替@Status.status
它应该是[@Status].status
或使用别名。< / p>
您可以非常简单地重新创建:
DECLARE @P TABLE (ID INT);
SELECT @P.ID FROM @P;
引发错误:
必须声明标量变量“@P”。
我也认为你没有正确使用这些参数,我认为你的程序应该是:
ALTER PROCEDURE MyPROC
@phoneNo nvarchar(30) = NULL,
@status dbo.StatusViewTableType READONLY
AS
BEGIN
SELECT *
FROM dbo.Customer WITH (NOLOCK)
INNER JOIN dbo.ContactNo WITH (NOLOCK)
ON dbo.Customer.id = dbo.ContactNo.CustomerID
WHERE ContactNo = @phoneNo
AND dbo.ContactNo.Contactability IN (SELECT s.status FROM @status AS s);
END
最后,我会强烈建议using SELECT *
in production code
修改强>
关于动态SQL问题,使用允许您使用参数的sp_executesql
,而不是像:
DECLARE @SQL NVARCHAR(MAX) = 'SELECT A, B, C FROM T WHERE 1 = 1',
@P NVARCHAR(10) = 'X';
IF @P <> ''
SET @SQL = @SQL + ' AND A = ''' + @P + '''';
EXEC(@SQL);
你会用
DECLARE @SQL NVARCHAR(MAX) = 'SELECT A, B, C FROM T WHERE 1 = 1',
@P NVARCHAR(10) = 'X';
IF @P <> ''
SET @SQL = @SQL + ' AND A = @P';
EXECUTE sp_executesql @SQL, N'@P NVARCHAR(10), @P;
因此,您将@P作为参数传递给查询。这应该可以解决您的所有单引号问题
答案 1 :(得分:0)
问题是您使用了不需要的单引号。试试这个。
ALTER PROCEDURE Myproc @phoneNo NVARCHAR(30) = NULL,
@status dbo.STATUSVIEWTABLETYPE READONLY
AS
BEGIN
SELECT *
FROM dbo.Customer WITH (NOLOCK)
INNER JOIN dbo.ContactNo WITH (NOLOCK)
ON dbo.Customer.id = dbo.ContactNo.CustomerID
WHERE ContactNo = @phoneNo
AND dbo.ContactNo.Contactability IN (SELECT status
FROM @status)
END
答案 2 :(得分:0)
动态SQL无法识别其范围之外的变量。这就是错误的原因。