| |

VerySource

 Forgot password?
 Register
Search
View: 652|Reply: 9

A long-troubled sql stored procedure (syntax error when converting to a column of data type int)

[Copy link]

1

Threads

5

Posts

5.00

Credits

Newbie

Rank: 1

Credits
5.00

 China

Post time: 2020-2-18 08:30:01
| Show all posts |Read mode
CREATE PROCEDURE GetDisplayNameByBatchAttibuteValues
(
@ListItemValue varchar (8000),
@ListDisplayName varchar (8000) output
)
AS

begin
declare @str varchar (8000)

 set @str = ''
 select @str = @str + ',' + ListDisplayName from ProductAttributeLookupListItems where ListItemValue in (@ListItemValue)

 set @ ListDisplayName = stuff (@ str, 1,1, '')
print @ListDisplayName
end
GO

@ListItemValue passes substrings like 14, 18, 22
error:
System.Data.SqlClient.SqlException: A syntax error occurred while converting the varchar value '14, 18,22 'to a column of data type int.

Please enlighten me! Thanks for your tears ~
Reply

Use magic Report

0

Threads

41

Posts

13.00

Credits

Newbie

Rank: 1

Credits
13.00

 China

Post time: 2020-4-22 14:45:01
| Show all posts
What type of ListItemValue is in the database?
Reply

Use magic Report

1

Threads

5

Posts

5.00

Credits

Newbie

Rank: 1

Credits
5.00

 China

 Author| Post time: 2020-4-22 23:30:01
| Show all posts
ListItemValue integer type, @ListItemValue is varchar (8000) passing multiple values, it seems that it will automatically become ('14, 18,22 ') in in, which is illegal, it should be in (14,18, twenty two)
How can I remove the automatic two `` number '', I have tried to replace it, and there is no good way, thank you for your advice
Reply

Use magic Report

0

Threads

41

Posts

13.00

Credits

Newbie

Rank: 1

Credits
13.00

 China

Post time: 2020-4-23 14:15:01
| Show all posts
CREATE PROCEDURE GetDisplayNameByBatchAttibuteValues
(
@ListItemValue varchar (8000),
@ListDisplayName varchar (8000) output
)
AS

begin
declare @str varchar (8000)


declare @tb table (a int)
while charindex (',', @ s)> 0
begin
 insert @tb values ​​(cast (left (@ s, charindex (',', @ ListItemValue) -1) as int))
 set @s = stuff (@ListItemValue, 1, charindex (',', @ ListItemValue), '')
end
insert @tb values ​​(@ListItemValue)

 set @str = ''
 select @str = @str + ',' + ListDisplayName from ProductAttributeLookupListItems where ListItemValue in (select a from @tb)

 set @ ListDisplayName = stuff (@ str, 1,1, '')
print @ListDisplayName
end
GO
Reply

Use magic Report

0

Threads

41

Posts

13.00

Credits

Newbie

Rank: 1

Credits
13.00

 China

Post time: 2020-4-23 15:30:02
| Show all posts
Use the following:


CREATE PROCEDURE GetDisplayNameByBatchAttibuteValues
(
@ListItemValue varchar (8000),
@ListDisplayName varchar (8000) output
)
AS

begin
declare @str varchar (8000)


declare @tb table (a int)
while charindex (',', @ ListItemValue)> 0
begin
 insert @tb values ​​(cast (left (@ s, charindex (',', @ ListItemValue) -1) as int))
 set @s = stuff (@ListItemValue, 1, charindex (',', @ ListItemValue), '')
end
insert @tb values ​​(@ListItemValue)

 set @str = ''
 select @str = @str + ',' + ListDisplayName from ProductAttributeLookupListItems where ListItemValue in (select a from @tb)

 set @ ListDisplayName = stuff (@ str, 1,1, '')
print @ListDisplayName
end
GO
Reply

Use magic Report

1

Threads

5

Posts

5.00

Credits

Newbie

Rank: 1

Credits
5.00

 China

 Author| Post time: 2020-4-24 03:15:01
| Show all posts
Thank you,
Where set @s is undefined
Is it @str?
Reply

Use magic Report

0

Threads

41

Posts

13.00

Credits

Newbie

Rank: 1

Credits
13.00

 China

Post time: 2020-4-24 20:45:01
| Show all posts
CREATE PROCEDURE GetDisplayNameByBatchAttibuteValues
(
@ListItemValue varchar (8000),
@ListDisplayName varchar (8000) output
)
AS

begin
declare @str varchar (8000)


declare @tb table (a int)
while charindex (',', @ ListItemValue)> 0
begin
 insert @tb values ​​(cast (left (@ ListItemValue, charindex (',', @ ListItemValue) -1) as int))
 set @s = stuff (@ListItemValue, 1, charindex (',', @ ListItemValue), '')
end
insert @tb values ​​(@ListItemValue)

 set @str = ''
 select @str = @str + ',' + ListDisplayName from ProductAttributeLookupListItems where ListItemValue in (select a from @tb)

 set @ ListDisplayName = stuff (@ str, 1,1, '')
print @ListDisplayName
end
GO
Reply

Use magic Report

0

Threads

41

Posts

13.00

Credits

Newbie

Rank: 1

Credits
13.00

 China

Post time: 2020-4-24 21:30:02
| Show all posts
Sorry, no mode
Reply

Use magic Report

1

Threads

5

Posts

5.00

Credits

Newbie

Rank: 1

Credits
5.00

 China

 Author| Post time: 2020-4-25 12:45:01
| Show all posts
Should be right
CREATE PROCEDURE GetDisplayNameByBatchAttibuteValues
(
@ListItemValue varchar (8000),
@ListDisplayName varchar (8000) output
)
AS

begin
declare @str varchar (8000)


declare @tb table (a int)
while charindex (',', @ ListItemValue)> 0
begin
 insert @tb values ​​(cast (left (@ ListItemValue, charindex (',', @ ListItemValue) -1) as int))
 set @ListItemValue = stuff (@ListItemValue, 1, charindex (',', @ ListItemValue), '')
end
insert @tb values ​​(@ListItemValue)

 set @str = ''
 select @str = @str + ',' + ListDisplayName from ProductAttributeLookupListItems where ListItemValue in (select a from @tb)

 set @ ListDisplayName = stuff (@ str, 1,1, '')

end
GO

But no result is returned, it is very strange
Reply

Use magic Report

1

Threads

5

Posts

5.00

Credits

Newbie

Rank: 1

Credits
5.00

 China

 Author| Post time: 2020-4-25 16:15:01
| Show all posts
Thanks tolisongfox, the problem is solved.
Reply

Use magic Report

You have to log in before you can reply Login | Register

Points Rules

Contact us|Archive|Mobile|CopyRight © 2008-2023|verysource.com ( 京ICP备17048824号-1 )

Quick Reply To Top Return to the list