VerySource

 找回密码
 立即注册
搜索
热搜: 活动 交友 discuz
查看: 964|回复: 8

存储过程调用自定义函数的问题

[复制链接]

2

主题

6

帖子

6.00

积分

新手上路

Rank: 1

积分
6.00
发表于 2020-2-23 00:30:01 | 显示全部楼层 |阅读模式
存储过程如下:
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,Beisen_EP_Users as d where c.Users_Id=b.EP_LogUsersId and b.EP_LogTestId=a.Test_Id and b.EP_LogEPUsersId=d.EP_UsersId 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,Beisen_EP_Users as d where c.Users_Id=b.EP_LogUsersId and b.EP_LogTestId=a.Test_Id and b.EP_LogEPUsersId=d.EP_UsersId 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,Beisen_EP_Users as d where c.Users_Id=b.EP_LogUsersId and b.EP_LogTestId=a.Test_Id and b.EP_LogEPUsersId=d.EP_UsersId 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
自定义函数如下:
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 其他的类似
数据库字段EP_LogTestTypePerson的值类似36.2|74.5|8.7|60|63.2|68.6|83.1|84|37.3|17|45.9
   调用的时候提示错误是:向 substring 函数传递了无效的 length 参数。
各位给点意见阿
回复

使用道具 举报

0

主题

4

帖子

5.00

积分

新手上路

Rank: 1

积分
5.00
发表于 2020-5-5 02:45:01 | 显示全部楼层
SUBSTRING(@str,@start,@end-@start)
上面这个函数有问题
@end-@start这个你最好用个变量保存计算出来的值然后再传到函数中去
回复

使用道具 举报

2

主题

6

帖子

6.00

积分

新手上路

Rank: 1

积分
6.00
 楼主| 发表于 2020-5-6 11:15:01 | 显示全部楼层
re:ellerywang
@end-@start不是变量吗?能说的清楚点吗?
我也发现SUBSTRING(@str,@start,@end-@start)出的问题 直接执行这个自定义函数的时候最后以行会出现乱码,说清方案阿。。。。
回复

使用道具 举报

0

主题

29

帖子

16.00

积分

新手上路

Rank: 1

积分
16.00
发表于 2020-5-7 09:15:01 | 显示全部楼层
print SUBSTRING(@str,@start,@end-@start)
先打出来看看啊..到底是什么时候出错了...

回复

使用道具 举报

0

主题

58

帖子

32.00

积分

新手上路

Rank: 1

积分
32.00
发表于 2020-5-10 09:00:01 | 显示全部楼层
先看看@start, @end-@start的值是多少, 参数传了不对。
回复

使用道具 举报

2

主题

6

帖子

6.00

积分

新手上路

Rank: 1

积分
6.00
 楼主| 发表于 2020-5-13 06:45:01 | 显示全部楼层
re:rcloud 我直接调用的自定义函数,不知道怎么打印。。汗。
re:lzzyfish @start, @end-@start的值有浮点型的,有整型的
回复

使用道具 举报

0

主题

29

帖子

16.00

积分

新手上路

Rank: 1

积分
16.00
发表于 2020-5-18 21:30:01 | 显示全部楼层
re:rcloud 我直接调用的自定义函数,不知道怎么打印。。汗。
-----------------------------------------------------------
你把他写成sql语句测试不行啊..非要在函数里测啊..
回复

使用道具 举报

0

主题

29

帖子

16.00

积分

新手上路

Rank: 1

积分
16.00
发表于 2020-5-19 11:00:02 | 显示全部楼层
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))

结果
-------------
18
22
63.2
回复

使用道具 举报

2

主题

6

帖子

6.00

积分

新手上路

Rank: 1

积分
6.00
 楼主| 发表于 2020-5-29 23:30:01 | 显示全部楼层
能把数据库字段全部打印出来吗?
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

Archiver|手机版|CopyRight © 2008-2023|verysource.com ( 京ICP备17048824号-1 )

快速回复 返回顶部 返回列表