-3

我要做的是基于具有相应数据库名称(@db_name)的company_id(@company_id),尝试根据这些变量提取数据。我有 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。

4

2 回答 2

0

我的问题是我试图在内部引用 company 表db_nameJOIN获取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)
于 2012-09-26T15:42:32.650 回答
-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)
于 2012-09-24T21:03:32.513 回答