这个查询需要动态sql吗? SQL服务器

发布于 2024-12-07 10:31:23 字数 1749 浏览 0 评论 0原文

我想选择一个存储过程的参数,然后在循环中检查每个参数是否为空。

我遇到的问题是,当我想计算 IF 语句中的变量时,它没有被正确评估。我想评估存储在表变量中的每个参数。我不知道这个的语法......或者也许这是不可能的? 这需要动态 SQL 吗?如果是这样,在执行动态sql时,变量的值将超出范围,那么我该如何处理?

我验证了 IF 语句之前的所有内容是否有效。

CREATE PROCEDURE dbo.UpdateBank
(
    @BankKey [smallint] = NULL,
    @Bank [varchar] (30) = NULL,
    @BankCode [char] (4) = NULL,
    @MasterBankCode [char] = NULL,
    @Bin [char] (6) = NULL,
    @WebSite [varchar] (50) = NULL,
    @isActive [bit] = NULL,
    @CreateDate [smalldatetime] = NULL
)
AS
SET NOCOUNT ON
SET ANSI_NULLS OFF

DECLARE @MaxRow TINYINT, @Count TINYINT
DECLARE @SPName VARCHAR (128), @CurrentRow TINYINT
SET @SPName = OBJECT_NAME(@@PROCID) -- SP self-reference to find its current name

DECLARE @SPParametersList TABLE (ID INT Identity(1,1) Primary Key,
                                 ParameterName NVARCHAR (128), 
                                 DataType NVARCHAR (128),
                                 ParameterMode NVARCHAR (10))

CREATE TABLE #TempExec(ID INT Identity(1,1) Primary Key,
                       Num BIT)


    INSERT INTO @SPParametersList (ParameterName, Datatype, ParameterMode)
        SELECT PARAMETER_NAME,DATA_TYPE,PARAMETER_MODE
        FROM INFORMATION_SCHEMA.PARAMETERS
        WHERE SPECIFIC_NAME = @SPName


    SET @CurrentRow = 1
    SELECT @MaxRow = ISNULL(MAX(ID),0) FROM @SPParametersList

    WHILE @CurrentRow <= @MaxRow
    BEGIN
        IF ((SELECT ParameterName FROM @SPParametersList WHERE ID = @CurrentRow) <> NULL)
        BEGIN
            SELECT 'Success' 
            SET @Count = @Count + 1 
        END
            SELECT 'Fail' 
        SET @CurrentRow = @CurrentRow + 1 

    END 

    SELECT @Count 

当我运行这个存储过程时,我总是得到“失败”

I want to select the parameters of a stored procedure and then check whether each parameter is null or not in a loop.

The problem I'm having is that when I want to evaluate the variable in the IF statement, it is not being evaluated correctly. I want to evaluate each parameter I stored in the table variable. I don't know the syntax for this...or maybe it isn't even possible?
Does this require Dynamic SQL? If so, when executing dynamic sql, the value of the variable will be out of scope so how do I deal with that?

I verified everything up to the IF statement works.

CREATE PROCEDURE dbo.UpdateBank
(
    @BankKey [smallint] = NULL,
    @Bank [varchar] (30) = NULL,
    @BankCode [char] (4) = NULL,
    @MasterBankCode [char] = NULL,
    @Bin [char] (6) = NULL,
    @WebSite [varchar] (50) = NULL,
    @isActive [bit] = NULL,
    @CreateDate [smalldatetime] = NULL
)
AS
SET NOCOUNT ON
SET ANSI_NULLS OFF

DECLARE @MaxRow TINYINT, @Count TINYINT
DECLARE @SPName VARCHAR (128), @CurrentRow TINYINT
SET @SPName = OBJECT_NAME(@@PROCID) -- SP self-reference to find its current name

DECLARE @SPParametersList TABLE (ID INT Identity(1,1) Primary Key,
                                 ParameterName NVARCHAR (128), 
                                 DataType NVARCHAR (128),
                                 ParameterMode NVARCHAR (10))

CREATE TABLE #TempExec(ID INT Identity(1,1) Primary Key,
                       Num BIT)


    INSERT INTO @SPParametersList (ParameterName, Datatype, ParameterMode)
        SELECT PARAMETER_NAME,DATA_TYPE,PARAMETER_MODE
        FROM INFORMATION_SCHEMA.PARAMETERS
        WHERE SPECIFIC_NAME = @SPName


    SET @CurrentRow = 1
    SELECT @MaxRow = ISNULL(MAX(ID),0) FROM @SPParametersList

    WHILE @CurrentRow <= @MaxRow
    BEGIN
        IF ((SELECT ParameterName FROM @SPParametersList WHERE ID = @CurrentRow) <> NULL)
        BEGIN
            SELECT 'Success' 
            SET @Count = @Count + 1 
        END
            SELECT 'Fail' 
        SET @CurrentRow = @CurrentRow + 1 

    END 

    SELECT @Count 

I always get 'Fail' when I run this stored proc

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

我家小可爱 2024-12-14 10:31:23

将行:更改

IF ((SELECT ParameterName FROM @SPParametersList WHERE ID = @CurrentRow) <> NULL)

IF ((SELECT ParameterName FROM @SPParametersList WHERE ID = @CurrentRow) IS NOT NULL)

您还需要将 @Count 变量初始化为 0:

    SET @Count = 0
    SET @CurrentRow = 1
    SELECT @MaxRow = ISNULL(MAX(ID),0) FROM @SPParametersList

Change your line:

IF ((SELECT ParameterName FROM @SPParametersList WHERE ID = @CurrentRow) <> NULL)

to

IF ((SELECT ParameterName FROM @SPParametersList WHERE ID = @CurrentRow) IS NOT NULL)

You also need to initialize the @Count variable to 0:

    SET @Count = 0
    SET @CurrentRow = 1
    SELECT @MaxRow = ISNULL(MAX(ID),0) FROM @SPParametersList
赏烟花じ飞满天 2024-12-14 10:31:23

问题可能出在 <> NULL 应该

 IF EXISTS(SELECT ParameterName FROM @SPParametersList WHERE ID = @CurrentRow) 

但我不确定你想用这段代码实现什么......

Probably the issue is in the <> NULL which should

 IF EXISTS(SELECT ParameterName FROM @SPParametersList WHERE ID = @CurrentRow) 

but I'm not sure what you want to achieve with that piece of code...

嘿嘿嘿 2024-12-14 10:31:23

我怀疑这是一个可以在不使用循环/游标的情况下重写的查询示例(根据我的经验,大多数sql...)

下面的查询是否给出了您想要的结果?

with temp as
(
    SELECT '@BankKey' as ParamName
        UNION
        SELECT '@Bank'
)

SELECT COUNT(*) as myCount
        FROM INFORMATION_SCHEMA.PARAMETERS as isp
        LEFT JOIN temp as t
        ON t.ParamName = isp.PARAMETER_NAME
        WHERE SPECIFIC_NAME = @SPName AND t.ParamName is null

您应该尽量避免使用循环/游标。 SQL Server(以及大多数 DBMS)在执行基于集合的操作方面表现出色,但在执行基于行的操作(循环/游标)方面表现不佳。

I suspect that this is an example of a query that can be rewritten without the use of loops/cursors (most sql is in my experience...)

Does the query below give you your desired results?

with temp as
(
    SELECT '@BankKey' as ParamName
        UNION
        SELECT '@Bank'
)

SELECT COUNT(*) as myCount
        FROM INFORMATION_SCHEMA.PARAMETERS as isp
        LEFT JOIN temp as t
        ON t.ParamName = isp.PARAMETER_NAME
        WHERE SPECIFIC_NAME = @SPName AND t.ParamName is null

You should try to avoid using Loops/Cursors as much as possible. SQL Server (and most DBMSs in general) are excellent at performing Set based operations and terrible at performing row based operations (loops/cursors).

祁梦 2024-12-14 10:31:23

(1)“当我运行此存储过程时,我总是得到'失败'”:您忘记了ELSE分支

IF ((SELECT ParameterName FROM @SPParametersList WHERE ID = @CurrentRow) /*<>*/ IS NOT NULL)
BEGIN
    SELECT 'Success' 
    SET @Count = @Count + 1 
END
ELSE -- << here
BEGIN
    SELECT 'Fail' 
END
SET @CurrentRow = @CurrentRow + 1

(2 ) 计算所有 not null 参数:

SELECT  @Count=COUNT(*)
FROM    @SPParametersList a
WHERE   a.ParameterName IS NOT NULL 

计算所有 null 参数:

SELECT  @Count=COUNT(*)
FROM    @SPParametersList a
WHERE   a.ParameterName IS NULL 

注意:如果您想测试 NULL / NOT NULL 你应该使用column/@variable IS [NOT] NULL 运算符和 SET ANSI_NULLS 必须为 ON:SET ANSI_NULLS ON

(1) "I always get 'Fail' when I run this stored proc": you have forget the ELSE branch

IF ((SELECT ParameterName FROM @SPParametersList WHERE ID = @CurrentRow) /*<>*/ IS NOT NULL)
BEGIN
    SELECT 'Success' 
    SET @Count = @Count + 1 
END
ELSE -- << here
BEGIN
    SELECT 'Fail' 
END
SET @CurrentRow = @CurrentRow + 1

(2) To count all not null parameters:

SELECT  @Count=COUNT(*)
FROM    @SPParametersList a
WHERE   a.ParameterName IS NOT NULL 

To count all null parameters:

SELECT  @Count=COUNT(*)
FROM    @SPParametersList a
WHERE   a.ParameterName IS NULL 

Note: if you want to test for NULL / NOT NULL you should use column/@variable IS [NOT] NULL operator and SET ANSI_NULLS must be ON: SET ANSI_NULLS ON.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文