简体   繁体   中英

How to do this in stored procedure?

Here's my stored procedure

CREATE PROCEDURE Test_SP
        @from int,
        @to int,
        @year int,
        @office varchar(MAX),
        @fund varchar(MAX)
AS             
SELECT        
        AccntTbl.Id,
         AccntTbl.accnt,
          ISNULL(SupplyTbl.Supply, 0) AS Supply,
           AccntTbl.office,
            AccntTbl.exp,                 
        AccntTbl.dateCreated 
FROM         AccntTbl 
LEFT OUTER JOIN
     (SELECT idAccnt, 
             SUM(amount) AS Supply
     FROM      SuppyTbl AS SupplyTbl1
     WHERE      (MONTH(dateCreated) BETWEEN @from AND @to) AND
                (YEAR(dateCreated) = @year) AND fund = @fund
     GROUP BY     idAccnt) AS SupplyTbl     
WHERE  YEAR(AccntTbl.dateCreated) = @year AND 
            AccntTbl.office = @office AND
             AccntTbl.fund = @fund 
GROUP BY AccntTbl.Id,
          AccntTbl.accnt,
            AccntTbl.appro,
              Supply.Supply

and here to call from VB.NET

  conn.Open()
    Dim cmd As New SqlCommand("Test_SP", conn)
    With cmd
        .CommandType = CommandType.StoredProcedure
        .Parameters.Add("@from", SqlDbType.VarChar).Value = Int32.Parse(TextBox1.Text)
        .Parameters.Add("@to", SqlDbType.VarChar).Value = Int32.Parse(TextBox2.Text)
        .Parameters.Add("@year", SqlDbType.VarChar).Value = Int32.Parse(YearLbl.Text)
        .Parameters.Add("@office", SqlDbType.VarChar).Value = Dashboard.OfficeTxt.Text
        .Parameters.Add("@fund", SqlDbType.VarChar).Value = FundLbl.Text
    End With

How can I retrieve just some of its field in the TEST_SP like I just only want AccntTbl.Accnt and ISNULL(SupplyTbl.Supply, 0) as supply

Here's the example code that I already do

Dim cmd As New SqlCommand("Select AccntTbl.Accnt, ISNULL(SupplyTbl.Supply, 0) as supply from Test_SP", conn)

I know it's wrong and it says 'Could not find stored procedure'... lol, glad for any help :)

One way is to use user defined table valued function :

CREATE FUNCTION [dbo].[Test_SP]
(       @from int,
        @to int,
        @year int,
        @office varchar(MAX),
        @fund varchar(MAX)
)
RETURNS TABLE
AS
RETURN (
  SELECT AccntTbl.Id,
         AccntTbl.accnt,
         ISNULL(SupplyTbl.Supply, 0) AS Supply,
         AccntTbl.office,
         AccntTbl.exp,                 
         AccntTbl.dateCreated 
  FROM AccntTbl 
  LEFT JOIN
     (SELECT idAccnt, 
             SUM(amount) AS Supply
     FROM SuppyTbl AS SupplyTbl1
     WHERE MONTH(dateCreated) BETWEEN @from AND @to 
       AND YEAR(dateCreated) = @year 
       AND fund = @fund
     GROUP BY idAccnt) AS SupplyTbl     
  WHERE YEAR(AccntTbl.dateCreated) = @year 
    AND AccntTbl.office = @office 
    AND AccntTbl.fund = @fund 
  GROUP BY AccntTbl.Id,
           AccntTbl.accnt,
           AccntTbl.appro,
           Supply.Supply
);

Query:

SELECT Accnt, Supply
FROM [dbo].[Test_SP](1, 12, 2015, 'Baltimore', 'aaa');

Note that conditions YEAR(dateCreated) = @year and MONTH(dateCreated) are nonSARGable so if there is index on dateCreated column query optimizer will skip it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM