在预期条件的上下文中指定的非布尔类型的表达式,接近... TABLE

时间:2012-09-24 20:47:24

标签: sql sql-server sql-server-2008

我要做的是基于company_id(@company_id),它具有相应的数据库名称(@db_name),试图根据这些变量提取数据。我有超过30个表,需要根据选择的company_id查看数据。

即。 EXEC journal_entries_by_dbase; 1'1'

    ALTER PROC esh_journal_entries_by_dbase
(@company_id varchar(2))
AS
-------------------------------------------------------
BEGIN
CREATE TABLE ##journal_dbase            
(company_id varchar(8)
, journal_description varchar(255)      
, journal_type varchar(8)
, date_entered varchar(10)
, date_applied varchar(10)
, date_posted varchar(10)
, hold_flag varchar(8)
, reversing_flag varchar(9)
, intercompany_flag varchar(8)
, domain_username varchar(255)
, posted_flag varchar(8)
, multi_currency_flag varchar(8)
, home_credit decimal(20,2)
, home_debit decimal(20,2))
;
-------------------------------------------------------
DECLARE --@company_id varchar(8)
        @db_name varchar(32)
        , @sql varchar(8000)
        , @drop_table varchar(200)
        ;
-------------------------------------------------------
SET @db_name = 'select db_name from contrl.dbo.company where company_id = 1' --@company_id';

-------------------------------------------------------
SET @sql =
'INSERT INTO ##journal_dbase
   SELECT '+@company_id+'  
   , trx.journal_description as [Description]
   , trx.journal_type as [Journal Code]
   , convert(varchar(12),dateadd(dd,(trx.date_entered - 639906),''1/1/1753''),101) as [Entry Date]
   , convert(varchar(12),dateadd(dd,(trx.date_applied - 639906),''1/1/1753''),101) as [Apply Date]
   , convert(varchar(12),dateadd(dd,(trx.date_posted - 639906),''1/1/1753''),101) as [Post Date]
   --, trx.date_entered as [Entry Date]
   --, trx.date_applied as [Apply Date]
   --, trx.date_posted as [Post Date]
   , [Hold] = CASE trx.hold_flag  
        WHEN 0 THEN ''No''  
        WHEN 1 THEN ''Yes'' 
        END --as hold
   , [Trans Flag] = CASE trx.reversing_flag  
        WHEN 0 THEN ''Standard'' 
        WHEN 1 THEN ''Reversing'' 
        END --as trans_flag
   , [InterCo] = CASE trx.intercompany_flag  
        WHEN 0 THEN ''No''  
        WHEN 1 THEN ''Yes''  
        END 
   , CASE WHEN e.domain_username NOT LIKE ''%\%''
        THEN e.domain_username
        ELSE SUBSTRING(e.domain_username,5,20)  --SELECT DISTINCT domain_username FROM ECTRL..smusers
        END AS [User Name]
   , [Posted Flag] = CASE trx.posted_flag  
        WHEN 0 THEN ''No''  
        WHEN 1 THEN ''Yes''  
        END 
   , trx.source_company_code as [Org]
   --, SUM(bal.home_credit) as [Total Home Credit] 
   --, SUM(bal.home_debit) as [Total Home Debit]    
   , bal.home_credit as [Total Home Credit]
   , bal.home_debit as [Total Home Debit]
FROM '+@db_name+'.dbo.trx trx                       
LEFT OUTER JOIN '+@db_name+'.dbo.trxdet trxdet
    ON trx.journal_ctrl_num = trxdet.journal_ctrl_num
LEFT OUTER JOIN '+@db_name+'.dbo.bal bal
    ON trxdet.account_code = bal.account_code
--LEFT OUTER JOIN con.dbo.users e
--  ON trx.user_id = e.user_id
--WHERE trx.date_posted > 0
--  AND trx.date_applied >= ''734503''
--  AND trx.date_applied <= ''734710'''
    ;
-------------------------------------------------------
SET @company_id = 'select company_id from ewcomp' --where db_name in (@db_name)
-------------------------------------------------------
EXEC (@sql);
-------------------------------------------------------
SELECT * FROM ##journal_dbase;

这是返回在预期条件的上下文中指定的非布尔类型的表达式,在bal和trx和trxdet附近。

2 个答案:

答案 0 :(得分:0)

我的问题是,当我需要将db_name发送到JOIN时,我试图在内部引用公司表格以获取sys.databases。我删除了SET @company_id,并将以下内容用于@db_name。有用。

SELECT @db_name = name 
FROM sys.databases 
    JOIN contrl.dbo.company 
    ON sys.databases.name = contrl.dbo.company.db_name 
WHERE contrl.dbo.company.company_id in (@company_id)

答案 1 :(得分:-1)

您将SQL查询放在公司的变量和数据库名称中。我认为您要查询表中的值:

SET @company = (SELECT company FROM contrl.dbo.company)
SET @db_name = (SELECT db_name FROM contrl.dbo.company WHERE company = @company)

或者简单地说:

SELECT @company = company FROM contrl.dbo.company
SELECT @db_name = db_name FROM contrl.dbo.company WHERE company = @company

编辑:

使用您更新的代码;改变这个:

SET @db_name = 'select db_name from contrl.dbo.company where company_id = 1'

SELECT @db_name = db_name from contrl.dbo.company where company_id = 1

或使用@CompnayId变量:

SELECT @db_name = db_name from contrl.dbo.company where company_id = cast(@CompanyId as int)