| |

VerySource

 Forgot password?
 Register
Search
View: 714|Reply: 8

The problem of calling a custom function from a stored procedure

[Copy link]

2

Threads

6

Posts

6.00

Credits

Newbie

Rank: 1

Credits
6.00

 China

Post time: 2020-2-23 00:30:01
| Show all posts |Read mode
The stored procedure is as follows:
 CREATE PROCEDURE sp_Motive
(

@EPUsersName nvarchar (50),
@Motive nvarchar (50),
@Type int
)
AS
DECLARE @StrSql nvarchar (4000)
IF (@ Type = 1)
BEGIN
SET @ StrSql = 'SELECT count (*) from BeiSen_EP_Log as b, BeiSen_Test as a, BeiSen_Users as c, Beiisen_EP_Users as d where c.Users_Id = b.EP_LogUsersId and b.EP_LogTestId = a.Test_Id and b.EP_LogEPUserIds = d. and d.EP_UsersName='''+@EPUsersName+ '' 'and b.EP_LogId> 3008 and b.EP_LogTestId = 42 and dbo.' + @ Motive + '(EP_LogTestTypePerson) <30'
EXEC sp_executesql @StrSql
END
ELSE IF (@ Type = 2)
BEGIN
SET @ StrSql = 'SELECT count (*) from BeiSen_EP_Log as b, BeiSen_Test as a, BeiSen_Users as c, Beiisen_EP_Users as d where c.Users_Id = b.EP_LogUsersId and b.EP_LogTestId = a.Test_Id and b.EP_LogEPUserIds = d. and d.EP_UsersName='''+@EPUsersName+ '' 'and b.EP_LogId> 3008 and b.EP_LogTestId = 42 and dbo.' + @ Motive + '(EP_LogTestTypePerson)> = 30 AND dbo.' + @ Motive + '(EP_LogTestTypePerson ) <= 70 '
EXEC sp_executesql @StrSql
END
ELSE IF (@ Type = 3)
BEGIN
SET @ StrSql = 'SELECT count (*) from BeiSen_EP_Log as b, BeiSen_Test as a, BeiSen_Users as c, Beiisen_EP_Users as d where c.Users_Id = b.EP_LogUsersId and b.EP_LogTestId = a.Test_Id and b.EP_LogEPUserIds = d. and d.EP_UsersName='''+@EPUsersName+ '' 'and b.EP_LogId> 3008 and b.EP_LogTestId = 42 and dbo.' + @ Motive + '(EP_LogTestTypePerson)> 70'
EXEC sp_executesql @StrSql
END
GO
The custom function is as follows:
 CREATE FUNCTION dbo.motivepower (@str nvarchar (50))
RETURNS int
BEGIN
DECLARE @start int
declare @end int
set @ start = charindex ('|', @ str, charindex ('|', @ str, charindex ('|', @ str, charindex ('|', @ str) +1) +1) +1) + 1
set @ end = charindex ('|', @ str, charindex ('|', @ str, charindex ('|', @ str, charindex ('|', @ str, charindex ('|', @ str) + 1) +1) +1) +1)
return convert (float, SUBSTRING (@ str, @ start, @ end- @ start))
END other
 The database field EP_LogTestTypePerson has a value similar to 36.2 | 74.5 | 8.7 | 60 | 63.2 | 68.6 | 83.1 | 84 | 37.3 | 17 | 45.9
   The error message when calling is that an invalid length parameter was passed to the substring function.
 Everyone give some advice
Reply

Use magic Report

0

Threads

4

Posts

5.00

Credits

Newbie

Rank: 1

Credits
5.00

 Invalid IP Address

Post time: 2020-5-5 02:45:01
| Show all posts
SUBSTRING (@ str, @ start, @ end- @ start)
There is a problem with this function
@ end- @ start, you better use a variable to save the calculated value and then pass it to the function
Reply

Use magic Report

2

Threads

6

Posts

6.00

Credits

Newbie

Rank: 1

Credits
6.00

 United States

 Author| Post time: 2020-5-6 11:15:01
| Show all posts
re:ellerywang
 Isn't @ end- @ start a variable? Can you make it clear?
 I also found out the problem with SUBSTRING (@ str, @ start, @ end- @ start). When this custom function is executed directly, garbled characters will appear at the end of the line. . . .
Reply

Use magic Report

0

Threads

29

Posts

16.00

Credits

Newbie

Rank: 1

Credits
16.00

 China

Post time: 2020-5-7 09:15:01
| Show all posts
print SUBSTRING (@ str, @ start, @ end- @ start)
Just type it out first ... when did it go wrong ...
Reply

Use magic Report

0

Threads

58

Posts

32.00

Credits

Newbie

Rank: 1

Credits
32.00

 China

Post time: 2020-5-10 09:00:01
| Show all posts
First look at @start, what is the value of @ end- @ start, the parameter is not correct.
Reply

Use magic Report

2

Threads

6

Posts

6.00

Credits

Newbie

Rank: 1

Credits
6.00

 China

 Author| Post time: 2020-5-13 06:45:01
| Show all posts
re:rcloudThe custom function I called directly, I don't know how to print. . sweat.
 re: lzzyfish @start, the value of @ end- @ start has floating point type and integer type
Reply

Use magic Report

0

Threads

29

Posts

16.00

Credits

Newbie

Rank: 1

Credits
16.00

 China

Post time: 2020-5-18 21:30:01
| Show all posts
re:rcloudThe custom function I called directly, I don't know how to print. . sweat.
-------------------------------------------------- ---------
You can write him as a sql statement to test it .. It must be tested in the function ..
Reply

Use magic Report

0

Threads

29

Posts

16.00

Credits

Newbie

Rank: 1

Credits
16.00

 China

Post time: 2020-5-19 11:00:02
| Show all posts
declare @str nvarchar (50)
set @ str = '36 .2 | 74.5 | 8.7 | 60 | 63.2 | 68.6 | 83.1 | 84 | 37.3 | 17 '
DECLARE @start int
declare @end int
set @ start = charindex ('|', @ str, charindex ('|', @ str, charindex ('|', @ str, charindex ('|', @ str) +1) +1) +1) + 1
print @start
set @ end = charindex ('|', @ str, charindex ('|', @ str, charindex ('|', @ str, charindex ('|', @ str, charindex ('|', @ str) + 1) +1) +1) +1)
print @end
print convert (float, SUBSTRING (@ str, @ start, @ end- @ start))

result
-------------
18
twenty two
63.2
Reply

Use magic Report

2

Threads

6

Posts

6.00

Credits

Newbie

Rank: 1

Credits
6.00

 China

 Author| Post time: 2020-5-29 23:30:01
| Show all posts
Can all the database fields be printed?
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