我有一个这样的存储过程:
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER PROCEDURE [dbo].[zsp_GetUsersAdministratorPanel]
(@StartRowIndex INT,
@MaxRows INT,
@OrderByField NVARCHAR(200),
@Asc BIT,
@SearchValue NVARCHAR(200))
AS
BEGIN
SET NOCOUNT ON;
DECLARE @UsersTable TABLE(UserId uniqueidentifier,
FirstName nvarchar(400),
LastName nvarchar(400),
Email nvarchar(200)
)
DECLARE @WhereQuery nvarchar(max)
SET @WhereQuery = ' WHERE '
IF LEN(@SearchValue) > 0
BEGIN
SET @WhereQuery = @WhereQuery +
'FirstName like ''%' + @SearchValue + '%'' or '
+ ' LastName like ''%' + @SearchValue + '%'' or '
+ ' Email like ''%' + @SearchValue + '%'''
END
DECLARE @OrderQuery nvarchar(max)
IF LEN(@OrderByField) > 0
BEGIN
IF @OrderByField = 'Email'
SET @OrderQuery = ' ORDER BY Email ' +
CASE @Asc WHEN 1 THEN ' ASC ' ELSE ' DESC ' END
ELSE IF @OrderByField = 'FirstName'
SET @OrderQuery = ' ORDER BY FirstName ' +
CASE @Asc WHEN 1 THEN ' ASC ' ELSE ' DESC ' END
ELSE IF @OrderByField = 'LastName'
SET @OrderQuery = ' ORDER BY LastName ' +
CASE @Asc WHEN 1 THEN ' ASC ' ELSE ' DESC ' END
END
DECLARE @Query NVARCHAR(MAX)
SET @Query = 'select UserId,
FirstName,
LastName,
Email
from
(
SELECT ROW_NUMBER() OVER ('+ @OrderQuery +')
AS [ROW_NUMBER],
[t0].[UserId], [t0].[FirstName],
[t0].[LastName],
[t0].[Email]
FROM [dbo].[Users] AS [t0]' +
@WhereQuery +
') AS [t1]' +
+ ' WHERE [ROW_NUMBER] BETWEEN
@StartRowIndex AND @MaxRows' + @OrderQuery
PRINT @Query
INSERT INTO @UsersTable
EXEC sp_Executesql @Query,
N'@OrderQuery nvarchar(max),
@StartRowIndex int,@MaxRows int',
@OrderQuery=@OrderQuery,
@StartRowIndex = @StartRowIndex,
@MaxRows = @MaxRows
SELECT * FROM @UsersTable
SET NOCOUNT OFF;
END
当我传递以下参数时:
StartRowIndex - 1
MaxRows(page size) - 50
OrderByField - FirstName
Asc - 1
SearchValue - Test
我收到了以下错误:
select UserId,
FirstName,
LastName,
Email
from
(
SELECT ROW_NUMBER() OVER ( ORDER BY FirstName ASC )
AS [ROW_NUMBER],
[t0].[UserId], [t0].[FirstName],
[t0].[LastName],
[t0].[Email]
FROM [dbo].[Users] AS [t0] WHERE FirstName like '%test%' or LastName like '%test%' or Email like '%test%') AS [t1] WHERE [ROW_NUMBER] BETWEEN
@StartRowIndex AND @MaxRows ORDER BY FirstName ASC
Msg 206,Level 16,State 2,Line 4
不兼容
操作数类型冲突:int与uniqueidentifier
我不太确定导致错误的原因是什么...我正在尝试创建一个存储过程,该过程将通过我的用户测试表,该测试表将包含大量数据(之后有500000个用户),所以我正在努力确保用户体验顺畅。
为什么我收到此错误?
有人能帮助我吗?
答案 0 :(得分:0)
简单的答案是int与uniqueidentifier的类型不同。这些实际上是两种不同的数据类型。
http://www.sqlteam.com/article/uniqueidentifier-vs-identity
解释uniqueidentifier是一个16字节的二进制值。
DECLARE @UsersTable TABLE(UserId uniqueidentifier,
应该是
DECLARE @UsersTable TABLE(UserId int,
回答您的问题的评论引发了其他程序的其他问题,但这特别针对您遇到的错误。