将数据插入数据库的问题

时间:2014-12-24 01:50:58

标签: sql sql-server

我正在尝试执行它的成功而没有任何错误,但该值未插入到dbo.Quarter_SourceData中。因此,我想知道是否有可能与多个存储过程合并?

我已经对与多个存储过程合并进行了一些研究,但是合并的代码似乎我并不理解。

USE [CBC_Quarter_Report]
GO
/****** Object:  StoredProcedure [dbo].[spDownloadQuarterSourceData]    Script Date: 12/24/2014 09:06:10 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER  PROCEDURE [dbo].[spDownloadQuarterSourceData]
AS
BEGIN
    SET NOCOUNT ON;

    DECLARE @sSQL nVarchar(Max) 
    DECLARE @sExe nVarchar(Max)
    DECLARE @QStartDate nvarchar(8)
    DECLARE @QEndDate nvarchar(8)

    SET @QStartDate = CONVERT(VARCHAR,(DATEADD(q, DATEDIFF(q, 0, getdate() - 8), 0)),112)
    SET @QEndDate = CONVERT(VARCHAR,(DATEADD(d, -1, DATEADD(q, DATEDIFF(q, 0, getdate() - 8) + 1, 0))), 112)
--------------------------------------------------------------------------------
-- Insert Client Info
--------------------------------------------------------------------------------

    SET @sSQL = 'SELECT DISTINCT AGNT.AGNTBR, AGNT.AGNTNUM, (CLNT.SURNAME + CLNT.GIVNAME) AS AGENT_NAME,
                IPI.Policy_No, IPI.Name AS Insured_Name,
                CONVERT(VARCHAR(10), CAST(IPI.Issue_Date AS DATE), 103) AS Date_CN_Issues,
                IPI.Inception_Date AS Contract_Commencement_Date, IPI.Expiry_Date,
                CASE WHEN PI.Transaction_Date IS NULL THEN '''' ELSE CONVERT(VARCHAR(10), CAST(PI.Transaction_Date AS DATE), 103) END AS Date_Received,
                DATEDIFF(DAY, IPI.Issue_Date, PI.Transaction_Date) AS Days_Compliant, DT_INSERT= getdate(),
                STATUS = NULL, ACTUAL_DAY = 0
                FROM dbo.Quarter_Insured_Policy AS IPI 
                LEFT JOIN dbo.AGNTPF AGNT ON IPI.Agent_No = AGNT.AGNTNUM
                LEFT JOIN dbo.CLNTPF CLNT ON AGNT.CLNTNUM = CLNT.CLNTNUM
                LEFT JOIN dbo.Quarter_Payment AS PI ON IPI.Policy_No = PI.Policy_No
                WHERE (PI.Transaction_Date >= ' + @QStartDate + ' AND PI.Transaction_Date <= ' + @QEndDate + ')
                AND substring(AGNT.AGNTNUM, 3,1) = ''V''
                ORDER BY  AGNT.AGNTBR, AGNT.AGNTNUM'

    TRUNCATE TABLE dbo.Quarter_SourceData

    set @sExe = 'Insert into dbo.Quarter_SourceData ' + @sSQL

    exec (@sExe)
    --print @sExe

END

1 个答案:

答案 0 :(得分:1)

这是你的主要问题。修改您的SET以在日期周围添加引号,因为它们是字符串,就像您对子字符串比较所做的那样。

SET @sSQL = 'SELECT DISTINCT AGNT.AGNTBR, AGNT.AGNTNUM, (CLNT.SURNAME + CLNT.GIVNAME) AS AGENT_NAME,
            IPI.Policy_No, IPI.Name AS Insured_Name,
            CONVERT(VARCHAR(10), CAST(IPI.Issue_Date AS DATE), 103) AS Date_CN_Issues,
            IPI.Inception_Date AS Contract_Commencement_Date, IPI.Expiry_Date,
            CASE WHEN PI.Transaction_Date IS NULL THEN '''' ELSE CONVERT(VARCHAR(10), CAST(PI.Transaction_Date AS DATE), 103) END AS Date_Received,
            DATEDIFF(DAY, IPI.Issue_Date, PI.Transaction_Date) AS Days_Compliant, DT_INSERT= getdate(),
            STATUS = NULL, ACTUAL_DAY = 0
            FROM dbo.Quarter_Insured_Policy AS IPI 
            LEFT JOIN dbo.AGNTPF AGNT ON IPI.Agent_No = AGNT.AGNTNUM
            LEFT JOIN dbo.CLNTPF CLNT ON AGNT.CLNTNUM = CLNT.CLNTNUM
            LEFT JOIN dbo.Quarter_Payment AS PI ON IPI.Policy_No = PI.Policy_No
            WHERE (PI.Transaction_Date >= ''' + @QStartDate + ''' AND PI.Transaction_Date <= ''' + @QEndDate + ''')
            AND substring(AGNT.AGNTNUM, 3,1) = ''V''
            ORDER BY  AGNT.AGNTBR, AGNT.AGNTNUM'

此外,最好使用EXEC sp_executesql (@sExe)代替EXEC (@sExe)