Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/sql-server-2005/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Sql server 2005 与sql中的Top有关_Sql Server 2005 - Fatal编程技术网

Sql server 2005 与sql中的Top有关

Sql server 2005 与sql中的Top有关,sql-server-2005,Sql Server 2005,我有数据类型int的变量@count。我正在将值设置为这个@count。 我想从表中选择top@count行数。当我使用Select top@count时,显示错误 IF (@NewItemCount<@OldItemCount) BEGIN set @count=@OldItemCount-@NewItemCount if(@count>0) BEGIN Delete from ItemDetail

我有数据类型int的变量@count。我正在将值设置为这个@count。 我想从表中选择top@count行数。当我使用Select top@count时,显示错误

  IF (@NewItemCount<@OldItemCount)
    BEGIN
        set @count=@OldItemCount-@NewItemCount
        if(@count>0)
        BEGIN
            Delete from ItemDetails where  GroupId in (Select Top @count  Id from ItemDetails where GroupId=@Prm_GroupId )
        END
    END
错误是

“@count”附近的语法不正确


在该点不可能使用变量


一种解决方案是使用动态sql:构建要在字符串变量中执行的完整查询字符串,并将其执行。

在此位置不可能使用变量


一种解决方案是使用动态sql:构建要在字符串变量中执行的完整查询字符串,并将其执行。

如果您要使用动态sql,我建议您阅读

编辑:

将@count变量包装在括号中应该对您有用:

 IF (@NewItemCount<@OldItemCount)
BEGIN
    set @count=@OldItemCount-@NewItemCount
    if(@count>0)
    BEGIN
        Delete from ItemDetails where  GroupId in (Select Top(@count)  Id from ItemDetails where GroupId=@Prm_GroupId )
    END
END

如果您打算使用动态SQL,我建议您阅读

编辑:

将@count变量包装在括号中应该对您有用:

 IF (@NewItemCount<@OldItemCount)
BEGIN
    set @count=@OldItemCount-@NewItemCount
    if(@count>0)
    BEGIN
        Delete from ItemDetails where  GroupId in (Select Top(@count)  Id from ItemDetails where GroupId=@Prm_GroupId )
    END
END

这在SQLServer2005上是开箱即用的,没有任何动态SQL。 你只是缺少括号。以下作品很有魅力:

DECLARE @CNT INT
SET @CNT = 5

SELECT  TOP (@CNT) *
FROM    MYTABLE

这在SQLServer2005上是开箱即用的,没有任何动态SQL。 你只是缺少括号。以下作品很有魅力:

DECLARE @CNT INT
SET @CNT = 5

SELECT  TOP (@CNT) *
FROM    MYTABLE

你想在这里做什么,这真的没有意义。。。在基本级别上,您需要在子语句中使用@Count=Id,但不确定这会起到什么作用…与lost一样-尝试在没有任何代码的情况下用简单的术语说明您正在做什么。您在这里尝试做什么,这真的没有意义。。。在基本级别上,您需要在子语句中使用@Count=Id,但不确定这会起到什么作用…与lost一样-尝试用简单的术语说明您正在做什么,而不使用任何代码。您真的有SQL Server 2005吗?您真的有SQL Server 2005吗?