这个查询需要动态sql吗? SQL服务器
我想选择一个存储过程的参数,然后在循环中检查每个参数是否为空。
我遇到的问题是,当我想计算 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
将行:更改
为
您还需要将 @Count 变量初始化为 0:
Change your line:
to
You also need to initialize the @Count variable to 0:
问题可能出在 <> NULL 应该
但我不确定你想用这段代码实现什么......
Probably the issue is in the <> NULL which should
but I'm not sure what you want to achieve with that piece of code...
我怀疑这是一个可以在不使用循环/游标的情况下重写的查询示例(根据我的经验,大多数sql...)
下面的查询是否给出了您想要的结果?
您应该尽量避免使用循环/游标。 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?
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).
(1)“当我运行此存储过程时,我总是得到'失败'”:您忘记了
ELSE
分支(2 ) 计算所有
not null
参数:计算所有
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(2) To count all
not null
parameters:To count all
null
parameters:Note: if you want to test for
NULL / NOT NULL
you should usecolumn/@variable IS [NOT] NULL
operator and SET ANSI_NULLS must be ON:SET ANSI_NULLS ON
.