1

我有一个用逗号分隔的 varchar 字符串,我想在我的 SQL 脚本中使用它,但我需要与数据库中的 bigint 字段进行比较。需要知道转换它:

DECLARE @RegionID varchar(200) = null
SET @RegionID = '853,834,16,467,841,460,495,44,859,457,437,836,864,434,86,838,458,472,832,433,142,154,159,839,831,469,442,275,840,299,446,220,300,225,227,447,301,450,230,837,441,835,302,477,855,411,395,279,303'

SELECT a.ClassAdID,    -- 1
        a.AdURL,        -- 2
        a.AdTitle,      -- 3
        a.ClassAdCatID, -- 4
        b.ClassAdCat,   -- 5
        a.Img1,         -- 6
        a.AdText,       -- 7
        a.MemberID,     -- 9
        a.Viewed,       -- 10
        c.Domain,       -- 11
        a.CreateDate    -- 12
        FROM ClassAd a  
        INNER JOIN ClassAdCat b ON b.ClassAdCAtID = a.ClassAdCAtID
        INNER JOIN Region c ON c.RegionID = a.RegionID
        AND a.PostType = 'CPN'
        AND DATEDIFF(d, GETDATE(), ExpirationDate) >= 0
        AND a.RegionID IN (@RegionID)
        AND Viewable = 'Y'

这失败并出现以下错误:

Error converting data type varchar to bigint.

RegionID 在数据库中是一个 bigint 字段.. 需要将 varchar 转换为 bigint.. 有什么想法..?

提前谢谢了,

新杰克

4

6 回答 6

4

创建这个函数:

CREATE function [dbo].[f_split]
(
@param nvarchar(max), 
@delimiter char(1)
)
returns @t table (val nvarchar(max), seq int)
as
begin
set @param += @delimiter

;with a as
(
select cast(1 as bigint) f, charindex(@delimiter, @param) t, 1 seq
union all
select t + 1, charindex(@delimiter, @param, t + 1), seq + 1
from a
where charindex(@delimiter, @param, t + 1) > 0
)
insert @t
select substring(@param, f, t - f), seq from a
option (maxrecursion 0)
return
end

改变这部分:

AND a.RegionID IN (select val from dbo.f_split(@regionID, ','))

更改此项以获得更好的整体性能:

AND DATEDIFF(d, 0, GETDATE()) <= ExpirationDate
于 2012-10-16T15:01:35.637 回答
1

您的查询不知道这些是单独的值,您可以为此使用动态 sql:

DECLARE @RegionID varchar(200) = null
SET @RegionID = '853,834,16,467,841,460,495,44,859,457,437,836,864,434,86,838,458,472,832,433,142,154,159,839,831,469,442,275,840,299,446,220,300,225,227,447,301,450,230,837,441,835,302,477,855,411,395,279,303'

declare @sql nvarchar(Max)

set @sql = 'SELECT a.ClassAdID,    -- 1
        a.AdURL,        -- 2
        a.AdTitle,      -- 3
        a.ClassAdCatID, -- 4
        b.ClassAdCat,   -- 5
        a.Img1,         -- 6
        a.AdText,       -- 7
        a.MemberID,     -- 9
        a.Viewed,       -- 10
        c.Domain,       -- 11
        a.CreateDate    -- 12
        FROM ClassAd a  
        INNER JOIN ClassAdCat b ON b.ClassAdCAtID = a.ClassAdCAtID
        INNER JOIN Region c ON c.RegionID = a.RegionID
        AND a.PostType = ''CPN''
        AND DATEDIFF(d, GETDATE(), ExpirationDate) >= 0
        AND a.RegionID IN ('+@RegionID+')
        AND Viewable = ''Y'''

exec sp_executesql @sql
于 2012-10-16T15:02:22.563 回答
1

我有时会使用这个方法,发现它非常好。它将逗号分隔的字符串转换为 AUX 表(称为#ARRAY),然后根据 AUX 表查询主表:

declare @RegionID varchar(50)
SET @RegionID = '853,834,16,467,841,460,495,44,859,457,437,836,864,434,86,838,458,472,832,433,142,154,159,839,831,469,442,275,840,299,446,220,300,225,227,447,301,450,230,837,441,835,302,477,855,411,395,279,303'
declare @S varchar(20)
if LEN(@RegionID) > 0 SET @RegionID = @RegionID + ',' 
CREATE TABLE #ARRAY(region_ID VARCHAR(20))

WHILE LEN(@RegionID) > 0 BEGIN
   SELECT @S = LTRIM(SUBSTRING(@RegionID, 1, CHARINDEX(',', @RegionID) - 1))
   INSERT INTO #ARRAY (region_ID) VALUES (@S)
   SELECT @RegionID = SUBSTRING(@RegionID, CHARINDEX(',', @RegionID) + 1, LEN(@RegionID))
END

select * from your_table
where regionID IN (select region_ID from #ARRAY)

它避免了您连接查询字符串然后使用 EXEC 执行它,我认为这不是一个很好的方法。

如果您需要两次运行代码,则需要删除临时表

于 2012-10-16T15:13:32.363 回答
0

我认为答案应该保持简单。
尝试像这样使用 CHARINDEX:

DECLARE @RegionID VARCHAR(200) = NULL 

SET @RegionID = 
'853,834,16,467,841,460,495,44,859,457,437,836,864,434,86,838,458,472,832,433,142,154,159,839,831,469,442,275,840,299,446,220,300,225,227,447,301,450,230,837,441,835,302,477,855,411,395,279,303'

SELECT 1 
WHERE  Charindex('834', @RegionID) > 0 

SELECT 1 
WHERE  Charindex('999', @RegionID) > 0 

CHARINDEX在大字符串变量中找到值时,它将返回它的位置,否则返回0。
将其用作搜索工具。

于 2012-10-16T15:19:59.510 回答
0

更改此查询的最简单方法是将IN函数替换为字符串函数。这是我认为最安全的方法LIKE(可在数据库之间移植):

    AND ','+@RegionID+',' like '%,'+cast(a.RegionID as varchar(255))+',%'

或者CHARINDEX

    AND charindex(','+cast(a.RegionID as varchar(255))+',', ','+@RegionID+',') > 0

但是,如果您明确地将列表放入代码中,为什么不使用临时表呢?

declare @RegionIds table (RegionId int);
insert into @RegionIds
    select 853 union all
    select 834 union all
    . . .
    select 303

然后您可以在IN子句中使用该表:

   AND a.RegionId in (select RegionId from @RegionIds)

或在一个JOIN子句中。

于 2012-10-16T15:27:34.170 回答
0

我喜欢 Diego 的回答,但我认为我的修改要好一些,因为您是在声明一个表变量而不是创建一个实际的表。我知道“in”语句可能有点慢,所以我做了一个内部连接,因为无论如何我都需要 Company 表中的一些信息。

declare @companyIdList varchar(1000)
set @companyIdList = '1,2,3'

if LEN(@companyIdList) > 0 SET @companyIdList = @companyIdList + ',' 
declare @CompanyIds TABLE (CompanyId bigint)
declare @S varchar(20)
WHILE LEN(@companyIdList) > 0 BEGIN
   SELECT @S = LTRIM(SUBSTRING(@companyIdList, 1, CHARINDEX(',', @companyIdList) - 1))
   INSERT INTO @CompanyIds (CompanyId) VALUES (@S)
   SELECT @companyIdList = SUBSTRING(@companyIdList, CHARINDEX(',', @companyIdList) + 1, LEN(@companyIdList))
END

select d.Id, d.Name, c.Id, c.Name 
from [Division] d 
    inner join [Company] c on d.CompanyId = c.Id
    inner join @CompanyIds cids on c.Id = cids.CompanyId
于 2013-06-17T18:15:29.350 回答