![]() | |
![]() |
| | Thread Tools | Display Modes |
#1
| |||
| |||
|
#2
| |||
| |||
|
|
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. |
#3
| |||
| |||
|
|
In my stored procedure, I want to parse @ArrayOfDays into @d1 through @d5 .. execute a dynamically built SELECT. |
#4
| |||
| |||
|
|
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. |
#5
| |||
| |||
|
|
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. |
|
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. |
#6
| |||
| |||
|
|
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 ---------------------------------------------------------- |
#7
| |||
| |||
|
|
I just got a little curious about how arrays could be implemented in T-SQL, .. |
#8
| |||
| |||
|
|
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. |
#9
| |||
| |||
|
#10
| |||
| |||
|
|
And yes, Tony... I'm after engineering, not quick and dirty code. Not sure who your criticism is aimed at, but I have been referring to an |
![]() |
| Thread Tools | |
| Display Modes | |
| |