我想将@ArrayOfDays解析为@ d1到@ d5 [英] I want to parse @ArrayOfDays into @d1 through @d5

查看:65
本文介绍了我想将@ArrayOfDays解析为@ d1到@ d5的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的存储过程中,我想将@ArrayOfDays解析为@ d1至

@ d5。


@ArrayOfDays是一个varchar输入参数,包含,

例如,1.7.21.25.60。。 - 五个元素。


最活跃的变量:

@i - 循环计数器

@char - 字符串中的当前字符

@tempVal - 包含正在构建的当前元素

@tempValExecString - 包含EXEC的SELECT stmt()


我'使用EXEC()执行动态构建的SELECT。


从vb.net调用时得到的错误是:

必须声明变量' '@tempVal''。


两条手动跟踪表明逻辑正常。


我怀疑我的@tempValExecString的赋值声明。 />

任何帮助将不胜感激。 - BobC


------------------------------------ ----------------------

DECLARE

@ d1 varchar(3),

@ d2 varchar(3),

@ d3 varchar(3),

@ d4 varchar(3),

@ d5 varchar(3),

@i int,

@char char(1),

@tempVal varchar(3),

@tempValExecString varchar(30)

SELECT @tempVal =''''

SELECT @i = 1


WHILE @ i< LEN(@ArrayOfDays)

BEGIN

SELECT @char = SUBSTRING(@ArrayOfDays,@ i,1)

WHILE @char<' '。''

BEGIN

SELECT @tempVal = @tempVal + @char

SELECT @char = SUBSTRING(@ArrayOfDays,@ i + 1,1)

IF @char =''。''

BEGIN

/ *以下应该产生SELECT @ d1 = 1 QUOT;当它读取

第一期(。)* /

SELECT @tempValExecString =''SELECT @d''+ LTRIM(RTRIM(STR(@i))) +''

= @tempVal''

EXEC(@tempValExecString)

SELECT @tempVal =''''

SELECT @i = @i + 1

END

SELECT @i = @i + 1

END

END

-------------------------------------- --------------------

In my stored procedure, I want to parse @ArrayOfDays into @d1 through
@d5.

@ArrayOfDays is a varchar input parameter containing,
for example, "1.7.21.25.60." - five elements.

Most active vars:
@i - loop counter
@char - current char in string
@tempVal - contains the current element as it is being built
@tempValExecString - contains SELECT stmt for EXEC()

I''m using EXEC() to execute a dynamically built SELECT.

The error I get when calling from vb.net is:
Must declare the variable ''@tempVal''.

Two manual traces indicate the logic is ok.

I suspect my assignment statement for @tempValExecString.

Any help would be appreciated. - BobC

----------------------------------------------------------
DECLARE
@d1 varchar(3),
@d2 varchar(3),
@d3 varchar(3),
@d4 varchar(3),
@d5 varchar(3),
@i int,
@char char(1),
@tempVal varchar(3),
@tempValExecString varchar(30)

SELECT @tempVal = ''''
SELECT @i = 1

WHILE @i < LEN(@ArrayOfDays)
BEGIN
SELECT @char = SUBSTRING(@ArrayOfDays, @i, 1)
WHILE @char <''.''
BEGIN
SELECT @tempVal = @tempVal + @char
SELECT @char = SUBSTRING(@ArrayOfDays, @i+1, 1)
IF @char = ''.''
BEGIN
/* the following should produce "SELECT @d1 = 1" when it reads the
first period(.) */
SELECT @tempValExecString = ''SELECT @d'' + LTRIM(RTRIM(STR(@i))) + ''
= @tempVal''
EXEC(@tempValExecString)
SELECT @tempVal = ''''
SELECT @i = @i + 1
END
SELECT @i = @i + 1
END
END
----------------------------------------------------------

推荐答案

bobc(bc ** ****@fmbnewhomes.com)写道:
bobc (bc******@fmbnewhomes.com) writes:

在我的存储过程中,我想通过

将@ArrayOfDays解析为@ d1 @ d5。

@ArrayOfDays是一个varchar输入参数,包含,例如,
,1.7.21.25.60。。 - 五个元素。


最活跃的变量:

@i - 循环计数器

@char - 字符串中的当前字符

@tempVal - 包含正在构建的当前元素

@tempValExecString - 包含EXEC的SELECT stmt()


我'使用EXEC()执行动态构建的SELECT。
In my stored procedure, I want to parse @ArrayOfDays into @d1 through
@d5.

@ArrayOfDays is a varchar input parameter containing,
for example, "1.7.21.25.60." - five elements.

Most active vars:
@i - loop counter
@char - current char in string
@tempVal - contains the current element as it is being built
@tempValExecString - contains SELECT stmt for EXEC()

I''m using EXEC() to execute a dynamically built SELECT.



等一下。你现在在关系数据库中,而不是在C ++程序中。


我没有问你为什么在

的程序中返回了分隔字符串你的第一篇文章,但是如果你打算在

调用程序中解压缩字符串,那么你就是错误的轨道altogther。将

数据传递到表中,并在整个集合上执行操作。

Wait a minute. You are in a relational database now, not in a C++ program.

I didn''t ask why you returned a delimited string in the procedure in
your first post, but if you intend on unpack the string in the
calling procedure, you are on the wrong track altogther. Pass the
data in a table, and perform your operations on the whole set.


从vb.net调用时出现的错误是:

必须声明变量''@ tempVal''。


两条手动跟踪表示逻辑正常。
The error I get when calling from vb.net is:
Must declare the variable ''@tempVal''.

Two manual traces indicate the logic is ok.



不,它不是。一批动态SQL是它自己的一个范围,而你的
不能访问外部范围内的变量。如果你想将@ b $ b变量@ d1分配给@ d5,那就是五个SELECT语句。


-

Erland Sommarskog,SQL Server MVP, es****@sommarskog.se


用于SQL Server 2005的联机书籍
http://www.microsoft.com/technet/pro...ads/books.mspx

SQL Server 2000联机丛书
http://www.microsoft.com/sql/prodinf...ons/books .mspx

No, it''s not. A batch of dynamic SQL is a scope of its own, and you
cannot access variables in outer scope. If you want to assign
variables @d1 to @d5, that''s five SELECT statements.

--
Erland Sommarskog, SQL Server MVP, es****@sommarskog.se

Books Online for SQL Server 2005 at
http://www.microsoft.com/technet/pro...ads/books.mspx
Books Online for SQL Server 2000 at
http://www.microsoft.com/sql/prodinf...ons/books.mspx


>在我的存储过程中,我想将@ArrayOfDays解析为@ d1到@ d5 ..执行动态构建的SELECT。 <


你几乎做错了什么。 SQL不能这样工作。

只需传递一个简单的五参数列表。然后在

过程体中清理数据。试试这个骨架


CREATE PROCEDURE Foobar

(@ d1 INTEGER,@ d2 INTEGER,@ d3 INTEGER,@ d4 INTEGER,@ d5 INTEGER)< br $>
AS

选择..

来自Floob

WHERE Floob.x IN(@ d1,@ d2,@ d3,@ d4,@ d5)

AND ..;


您可以在IN()列表中使用COALESCE来处理NULL或其他任何内容。 />
>In my stored procedure, I want to parse @ArrayOfDays into @d1 through @d5 .. execute a dynamically built SELECT. <<

You are doing almost everything wrong. SQL does not work this way.
Just pass a simple five parameter list. Then clean up your data in the
procedure body. Try this for a skeleton

CREATE PROCEDURE Foobar
(@d1 INTEGER, @d2 INTEGER, @d3 INTEGER, @d4 INTEGER, @d5 INTEGER)
AS
SELECT ..
FROM Floob
WHERE Floob.x IN (@d1, @d2, @d3, @d4, @d5)
AND ..;

YOU can use COALESCE in the IN() list to handle NULLs or whatever.


9月28日晚上8:52, - CELKO--< jcelko ... @ earthlink.netwrote:
On Sep 28, 8:52 pm, --CELKO-- <jcelko...@earthlink.netwrote:

在我的存储过程中,我想将@ArrayOfDays解析为@ d1到@ d5 ..执行动态构建的SELECT。 <<
In my stored procedure, I want to parse @ArrayOfDays into @d1 through @d5 .. execute a dynamically built SELECT. <<



你几乎所有的事都做错了。 SQL不能这样工作。

只需传递一个简单的五参数列表。然后在

过程体中清理数据。试试这个骨架


CREATE PROCEDURE Foobar

(@ d1 INTEGER,@ d2 INTEGER,@ d3 INTEGER,@ d4 INTEGER,@ d5 INTEGER)< br $>
AS

选择..

来自Floob

WHERE Floob.x IN(@ d1,@ d2,@ d3,@ d4,@ d5)

AND ..;


您可以在IN()列表中使用COALESCE来处理NULL或其他任何内容。


You are doing almost everything wrong. SQL does not work this way.
Just pass a simple five parameter list. Then clean up your data in the
procedure body. Try this for a skeleton

CREATE PROCEDURE Foobar
(@d1 INTEGER, @d2 INTEGER, @d3 INTEGER, @d4 INTEGER, @d5 INTEGER)
AS
SELECT ..
FROM Floob
WHERE Floob.x IN (@d1, @d2, @d3, @d4, @d5)
AND ..;

YOU can use COALESCE in the IN() list to handle NULLs or whatever.



谢谢,全部。这两个帖子(昨天和今天)已经成为我的b / b
实验的一部分,以找到查询大量

数据的最有效方法,以大量不同方式填充 ;仪表盘"页面上

我们的.net内部网。你们两个都发现我比sql程序员更像是一个应用程序员,所以我知道你会原谅我b $ b原谅我。同时,您的输入非常有帮助。谢谢

你的时间。 -BobC


ps:es,输出字符串array我打算用我的
vb.net应用解析。我对t-sql中实现的数组怎么样有点好奇,并且可能通过我的应用程序保存对数据库服务器的一些调用,或者至少减少批次数。我确定之前已经完成了所有的事情,但我必须为自己尝试失败。学习

经验,如果没有别的。再次感谢。

Thanks, all. Both posts (yesterday and today) have been part of my
experiments to find the most efficient method of querying a ton of
data a ton of different ways to populate a "dashboard" page on
our .net intranet. You''ve both discovered that I am more an
applications programmer than a sql programmer, so I know you''ll
forgive me. Meanwhile, your input has been very helpful. Thanks for
your time. -BobC

ps: es, the output string "array" was intended to be parsed by my
vb.net app. I just got a little curious about how arrays could be
implemented in t-sql, and possibly save some calls to the db server by
my app, or at least reduce the number of batches. I''m sure it''s all
been done before, but I had to try and fail for myself. A learning
experience if nothing else. Thanks again.


这篇关于我想将@ArrayOfDays解析为@ d1到@ d5的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆