wuguang1023 2011-01-05 16:53
浏览 255
已采纳

关于这个存储过程创建中不能顺利执行的问题

CREATE PROCEDURE GetHashCode
@input tAppName,
@hash INT OUTPUT
AS
/*
This sproc is based on this C# hash function:

int GetHashCode(string s)
{
int hash = 5381;
int len = s.Length;

for (int i = 0; i < len; i++) {
int c = Convert.ToInt32(s[i]);
hash = ((hash << 5) + hash) ^ c;
}

return hash;
}

However, SQL 7 doesn't provide a 32-bit integer
type that allows rollover of bits, we have to
divide our 32bit integer into the upper and lower
16 bits to do our calculation.
*/

DECLARE @hi_16bit INT
DECLARE @lo_16bit INT
DECLARE @hi_t INT
DECLARE @lo_t INT
DECLARE @len INT
DECLARE @i INT
DECLARE @c INT
DECLARE @carry INT

SET @hi_16bit = 0
SET @lo_16bit = 5381

SET @len = DATALENGTH(@input)
SET @i = 1

WHILE (@i <= @len)
BEGIN
SET @c = ASCII(SUBSTRING(@input, @i, 1))

/* Formula:
hash = ((hash << 5) + hash) ^ c */

/* hash << 5 /
SET @hi_t = @hi_16bit * 32 /
high 16bits << 5 /
SET @hi_t = @hi_t & 0xFFFF /
zero out overflow */

SET @lo_t = @lo_16bit * 32 /* low 16bits << 5 */

SET @carry = @lo_16bit & 0x1F0000 /* move low 16bits carryover to hi 16bits /
SET @carry = @carry / 0x10000 /
>> 16 /
SET @hi_t = @hi_t + @carry
SET @hi_t = @hi_t & 0xFFFF /
zero out overflow */

/* + hash /
SET @lo_16bit = @lo_16bit + @lo_t
SET @hi_16bit = @hi_16bit + @hi_t + (@lo_16bit / 0x10000)
/
delay clearing the overflow */

/* ^c */
SET @lo_16bit = @lo_16bit ^ @c

/* Now clear the overflow bits */

SET @hi_16bit = @hi_16bit & 0xFFFF
SET @lo_16bit = @lo_16bit & 0xFFFF

SET @i = @i + 1
END

/* Do a sign extension of the hi-16bit if needed */
IF (@hi_16bit & 0x8000 <> 0)
SET @hi_16bit = 0xFFFF0000 | @hi_16bit

/* Merge hi and lo 16bit back together /
SET @hi_16bit = @hi_16bit * 0x10000 /
<< 16 */
SET @hash = @hi_16bit | @lo_16bit

RETURN 0

GO

消息 2715,级别 16,状态 3,过程 GetHashCode,第 1 行
第 1 个列、参数或变量: 找不到数据类型 tAppName。
参数或变量 '@input' 的数据类型无效。
请问SQLSERVER 中没有这样的tAppName数据类型的我的,这个存储过程该怎么建立。

  • 写回答

2条回答 默认 最新

  • jiangguangxian 2011-01-06 12:37
    关注

    这个很明确,没有,sql里面varchar可以容纳int等参数类型,你就先已varchar形式传进来,然后真需要判断是什么类型的话,在传个类型参数来,然后到sql中再转换一下。

    [code="java"]CREATE PROCEDURE GetHashCode
    @input varchar(1000),
    @type varchar(10),
    @hash INT OUTPUT
    AS
    /*
    这里现在转换一下你需要的类型。
    /
    if @type='int'
    begin
    declare @inputParam int
    @inputParam = covert(int,@input)
    end
    else if @type='otherType'
    ....
    /

    This sproc is based on this C# hash function:

    int GetHashCode(string s)
    {
    int hash = 5381;
    int len = s.Length;

    for (int i = 0; i < len; i++) {
    int c = Convert.ToInt32(s[i]);
    hash = ((hash << 5) + hash) ^ c;
    }

    return hash;
    }

    However, SQL 7 doesn't provide a 32-bit integer
    type that allows rollover of bits, we have to
    divide our 32bit integer into the upper and lower
    16 bits to do our calculation.
    */

    DECLARE @hi_16bit INT
    DECLARE @lo_16bit INT
    DECLARE @hi_t INT
    DECLARE @lo_t INT
    DECLARE @len INT
    DECLARE @i INT
    DECLARE @c INT
    DECLARE @carry INT

    SET @hi_16bit = 0
    SET @lo_16bit = 5381

    SET @len = DATALENGTH(@input)
    SET @i = 1

    WHILE (@i <= @len)
    BEGIN
    SET @c = ASCII(SUBSTRING(@input, @i, 1))

    /* Formula:
    hash = ((hash << 5) + hash) ^ c */

    /* hash << 5 /
    SET @hi_t = @hi_16bit * 32 /
    high 16bits << 5 /
    SET @hi_t = @hi_t & 0xFFFF /
    zero out overflow */

    SET @lo_t = @lo_16bit * 32 /* low 16bits << 5 */

    SET @carry = @lo_16bit & 0x1F0000 /* move low 16bits carryover to hi 16bits /
    SET @carry = @carry / 0x10000 /
    >> 16 /
    SET @hi_t = @hi_t + @carry
    SET @hi_t = @hi_t & 0xFFFF /
    zero out overflow */

    /* + hash /
    SET @lo_16bit = @lo_16bit + @lo_t
    SET @hi_16bit = @hi_16bit + @hi_t + (@lo_16bit / 0x10000)
    /
    delay clearing the overflow */

    /* ^c */
    SET @lo_16bit = @lo_16bit ^ @c

    /* Now clear the overflow bits */

    SET @hi_16bit = @hi_16bit & 0xFFFF
    SET @lo_16bit = @lo_16bit & 0xFFFF

    SET @i = @i + 1
    END

    /* Do a sign extension of the hi-16bit if needed */
    IF (@hi_16bit & 0x8000 <> 0)
    SET @hi_16bit = 0xFFFF0000 | @hi_16bit

    /* Merge hi and lo 16bit back together /
    SET @hi_16bit = @hi_16bit * 0x10000 /
    << 16 */
    SET @hash = @hi_16bit | @lo_16bit

    RETURN 0

    GO [/code]

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 SQL Server下载
  • ¥15 python如何将动态的多个子列表,拼接后进行集合的交集
  • ¥20 vitis-ai量化基于pytorch框架下的yolov5模型
  • ¥15 如何实现H5在QQ平台上的二次分享卡片效果?
  • ¥15 python爬取bilibili校园招聘网站
  • ¥30 求解达问题(有红包)
  • ¥15 请解包一个pak文件
  • ¥15 不同系统编译兼容问题
  • ¥100 三相直流充电模块对数字电源芯片在物理上它必须具备哪些功能和性能?
  • ¥30 数字电源对DSP芯片的具体要求