2017-08-10 190 views
2

我在存儲過程中有一個SQL查詢,並且出現錯誤。C#&SQL Server:執行存儲過程

下面是該查詢:

IF EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[fPartnersDebt]') AND type in (N'FN', N'IF', N'TF', N'FS', N'FT')) 
    DROP FUNCTION [dbo].[fPartnersDebt] 

/****** Object: UserDefinedFunction [dbo].[fPartnersDebt] Script Date: 03/07/2012 16:02:01 ******/ 
SET ANSI_NULLS ON 

SET QUOTED_IDENTIFIER ON 

exec('CREATE PROCEDURE [dbo].[fPartnersDebt]') 
( 
    @StartDate datetime 
    ,@EndDate datetime 
    ,@CompanyName nvarchar(100) 
    ,@Account nvarchar(100) 
    ,@Reference nvarchar(max) 
    ,@ShowClosedDocuments bit 
) 
RETURNS TABLE 
AS 
    RETURN 
     (SELECT 
      t1.Name, t1.Bulstat, t1.Amount, t1.IsVat 
     FROM 
      (SELECT 
        c.CompanyID, c.Name AS [Name], c.IsVat, c.Bulstat AS [Bulstat], 
        SUM((ad.Amount + ad.RefAmount) * ad.[Sign]) AS [Amount] 
       FROM 
        Companies c 
       JOIN 
        AccountingDetails ad 
       JOIN 
        Accounts a 
       JOIN 
        AccountCategories ac ON ac.AccountCategoryID = a.AccountCategoryID 
        ON ad.AccountID = a.AccountID 
       JOIN 
        Accountings acc ON ad.AccountingID = acc.AccountingID 
        ON ad.CompanyID = c.CompanyID 
       LEFT JOIN 
        [References] r ON acc.OptionalReferenceID = r.ReferenceID 
       WHERE 
        ac.TypeIdentifier IN (4, 6, 7, 8, 9, 20, 33, 54, 10, 12, 13, 14, 15, 19, 34, 55) 
        AND Name LIKE @CompanyName 
        AND (a.Number = @Account OR a.Number LIKE @Account + '/%' OR (Len(@Account) < 3 AND a.Number LIKE @Account + '%')) 
        AND acc.AccountingDate >= @StartDate 
        AND acc.AccountingDate <= @EndDate 
        AND (ISNULL(r.[Description], '') LIKE @Reference + '%') 
       GROUP BY 
        c.CompanyID, c.Name, c.Bulstat, c.IsVat) t1 
    WHERE 
     @ShowClosedDocuments = 1 OR t1.Amount <> 0 
) 

錯誤:

附近有語法錯誤@StartDate「。

必須聲明標量變量「@CompanyName」。

我嘗試用很多方式聲明,但沒有任何不工作。對不起,但我是新手,解決這個問題有點困難。

回答

6

使用GO分離批次,刪除動態SQL和CREATE PROCEDURECREATE FUNCTION取代:

SET ANSI_NULLS ON 
SET QUOTED_IDENTIFIER ON 
GO 

IF EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[fPartnersDebt]') AND type in (N'FN', N'IF', N'TF', N'FS', N'FT')) 
DROP FUNCTION [dbo].[fPartnersDebt] 
GO  -- here 


CREATE FUNCTION [dbo].[fPartnersDebt] -- here 
( 
    @StartDate datetime 
    ,@EndDate datetime 
    ,@CompanyName nvarchar(100) 
    ,@Account nvarchar(100) 
    ,@Reference nvarchar(max) 
    ,@ShowClosedDocuments bit 
) 
RETURNS TABLE 
AS 
RETURN 
(
    SELECT 
     t1.Name 
     ,t1.Bulstat 
     ,t1.Amount 
     ,t1.IsVat 
    FROM 
     (SELECT 
      c.CompanyID 
      ,c.Name AS [Name] 
      ,c.IsVat 
      ,c.Bulstat AS [Bulstat] 
      ,SUM((ad.Amount + ad.RefAmount) * ad.[Sign]) AS [Amount] 
     FROM Companies c 
      JOIN AccountingDetails ad 
       JOIN Accounts a 
        JOIN AccountCategories ac ON ac.AccountCategoryID = a.AccountCategoryID 
       ON ad.AccountID = a.AccountID 
       JOIN Accountings acc ON ad.AccountingID = acc.AccountingID 
      ON ad.CompanyID = c.CompanyID 
      LEFT JOIN [References] r ON acc.OptionalReferenceID = r.ReferenceID 
     WHERE ac.TypeIdentifier IN (4,6,7,8,9,20,33,54,10,12,13,14,15,19,34,55) 
       AND Name LIKE @CompanyName 
       AND (a.Number = @Account OR a.Number LIKE @Account + '/%' OR (Len(@Account)<3 AND a.Number LIKE @Account + '%')) 
       AND acc.AccountingDate >= @StartDate AND acc.AccountingDate <= @EndDate 
       AND (ISNULL(r.[Description], '') LIKE @Reference + '%') 
     GROUP BY 
      c.CompanyID 
      ,c.Name 
      ,c.Bulstat 
      ,c.IsVat)t1 
    WHERE @ShowClosedDocuments = 1 OR t1.Amount <> 0 
) 

編輯:

GO是一批分離器,其內部工作SSMS。如果您想從C#執行它,請撥打兩個不同的電話。首先DROP FUNCTION然後重新創建它。

+0

錯誤: System.Data.SqlClient.SqlException(0x80131904):「GO」附近的語法錯誤。 'GO'附近語法不正確。 'CREATE FUNCTION'必須是查詢批處理中的第一條語句。 必須聲明標量變量「@CompanyName」。 –

+0

@ДимитърГрудев所以你從'C#'執行它,如果是的話,用兩個調用來調用它,而不是一個。 – lad2025

+0

是的,我用c#執行。對不起,但我是新的。你能用代碼告訴我嗎? –