SQL查询连接以创建文件路径

时间:2014-02-17 14:16:04

标签: sql sql-server-2008

我需要在SQL查询中从3列创建文件路径。一切都完成后,将在文件中使用。我尝试过使用CONCAT和字符串方法,但没有运气。查询如下。

SELECT 
    dbo.TBIndexData.DocGroup, 
    dbo.TBIndexData.Index1 AS Title, 
    dbo.TBIndexData.Index2 AS Meeting_Date, 
    dbo.TBIndexData.Index3 AS Meeting_Number,
    dbo.TBIndexData.Index4 AS Meeting_Type, 
    dbo.TBIndexData.Index5 AS Doc_Name, 
    dbo.TBIndexData.Index6 AS Doc_Type,
    dbo.TBIndexData.Index7 AS Meeting_Page, 
    dbo.TBIndexData.Index8 AS Notes, 
    dbo.TBIndexData.Index9 AS TBUser, 
    dbo.TBIndexData.Index10 AS Date_Scanned, 
    CONCAT (dbo.TBPrimary.FileDir + '\' + dbo.TBPrimary.TimsFileID + '.' + dbo.TBPrimary.FileExtension) AS FilePath
FROM 
    dbo.TBIndexData
JOIN 
    dbo.TBPrimary ON dbo.TBIndexData.DocGroup = dbo.TBPrimary.DocGroup

2 个答案:

答案 0 :(得分:2)

在SQL Server 2008中,您需要类似

的内容
SELECT I.DocGroup,
       I.Index1  AS Title,
       I.Index2  AS Meeting_Date,
       I.Index3  AS Meeting_Number,
       I.Index4  AS Meeting_Type,
       I.Index5  AS Doc_Name,
       I.Index6  AS Doc_Type,
       I.Index7  AS Meeting_Page,
       I.Index8  AS Notes,
       I.Index9  AS TBUser,
       I.Index10 AS Date_Scanned, 
       P.FileDir + '\' + CAST(P.TimsFileID AS VARCHAR(10)) + 
            '.' + P.FileExtension AS FilePath
FROM   dbo.TBIndexData I
       JOIN dbo.TBPrimary P
         ON I.DocGroup = P.DocGroup 

您不应在schemaname.tablename列表中使用SELECT。这是not officially supported grammar。只需使用tablename或提供别名。

(如果调用CLR数据类型列的属性,使用两个部件名称可能会导致混淆错误)

答案 1 :(得分:-1)

尝试使用带逗号的CONCAT

CONCAT (dbo.TBPrimary.FileDir, '\', dbo.TBPrimary.TimsFileID, '.', bo.TBPrimary.FileExtension) AS FilePath