1

I have following stored procedure. It generates ProductBalanceAndTurnover report for a period.

ALTER PROCEDURE [dbo].[ProductBalanceAndTurnover]
-- Add the parameters for the stored procedure here
@DateTime1 datetime,
@DateTime2 datetime
AS
BEGIN
-- SET NOCOUNT ON added to prevent extra result sets from
-- interfering with SELECT statements.
SET NOCOUNT ON;

-- Insert statements for procedure here

DROP TABLE IF EXISTS #Balance1;
DROP TABLE IF EXISTS #Balance2;
DROP TABLE IF EXISTS #In;
DROP TABLE IF EXISTS #Out;
DROP TABLE IF EXISTS #Items;

SELECT
    Product, Price, SUM(Quantity * InOut) AS Balance1
INTO
    #Balance1
FROM
    Reg_Warehouse
WHERE
    ([DateTime] < @DateTime1)
GROUP BY
    Product, Price
HAVING
    (SUM(Quantity * InOut) <> 0);

SELECT
    Product, Price, SUM(Quantity * InOut) AS Balance2
INTO
    #Balance2
FROM
    Reg_Warehouse
WHERE
    ([DateTime] < @DateTime2)
GROUP BY
    Product, Price
HAVING
    (SUM(Quantity * InOut) <> 0);

SELECT
    Product, Price, SUM(Quantity) AS [In]
INTO
    #In
FROM
    Reg_Warehouse
WHERE
    ([DateTime] >= @DateTime1 AND [DateTime] < @DateTime2 AND InOut = 1)
GROUP BY
    Product, Price
HAVING
    (SUM(Quantity) <> 0);

SELECT
    Product, Price, SUM(Quantity) AS [Out]
INTO
    #Out
FROM
    Reg_Warehouse
WHERE
    ([DateTime] >= @DateTime1 AND [DateTime] < @DateTime2 AND InOut = -1)
GROUP BY
    Product, Price
HAVING
    (SUM(Quantity) <> 0);


SELECT
    Product,
    Price
INTO
    #Items
FROM
(
    SELECT
        Product,
        Price
    FROM
        #Balance1
    UNION ALL
    SELECT
        Product,
        Price
    FROM
        #Balance2
    UNION ALL
    SELECT
        Product,
        Price
    FROM
        #In
    UNION ALL
    SELECT
        Product,
        Price
    FROM
        #Out
)
AS m
GROUP BY
    m.Product,
    m.Price;

SELECT
    #Items.Product,
    dbo.GetProductLongName(#Items.Product) AS ProductLongName,
    Unit.FullName AS Unit,
    #Items.Price,
    #Balance1.Balance1,
    #Balance1.Balance1 * #Items.Price AS Balance1V,
    #In.[In] AS [In],
    #In.[In] * #Items.Price AS [InV],
    #Out.[Out] AS [Out],
    #Out.[Out] * #Items.Price AS [OutV],
    #Balance2.Balance2,
    #Balance2.Balance2 * #Items.Price AS Balance2V
FROM
    #Items
    LEFT OUTER JOIN #Balance1 ON #Balance1.Product = #Items.Product AND #Balance1.Price = #Items.Price
    LEFT OUTER JOIN #Balance2 ON #Balance2.Product = #Items.Product AND #Balance2.Price = #Items.Price
    LEFT OUTER JOIN #In ON #In.Product = #Items.Product AND #In.Price = #Items.Price
    LEFT OUTER JOIN #Out ON #Out.Product = #Items.Product AND #Out.Price = #Items.Price
    LEFT OUTER JOIN Product ON Product.Id = #Items.Product
    LEFT OUTER JOIN Unit ON Unit.Id = Product.Unit;


END

In my WinForms project in Visual Studio I can't create a TableAdapter for this stored procedure. Is there a solution for it?

Edit

enter image description here enter image description here enter image description here

DotNet Developer
  • 2,973
  • 1
  • 15
  • 24
  • "I cant create a TableAdapter" doesn't tell us anything. Please describe specifically what you did and what happened as a consequence. – RBarryYoung Mar 16 '22 at 12:51
  • @RBarryYoung, see the edit – DotNet Developer Mar 16 '22 at 12:59
  • This is a spurious error on the Wizard's part. This is a waring thrown by SQL Server, but it can be safely ignored by the user, unfortunately the wizard is not allowing you to ignore it. Can you add it without the wizard (and just ignore the error)? – RBarryYoung Mar 16 '22 at 13:07
  • @RBarryYoung, ignoring the error is resulting in a table with no columns. VS is unable to retrieve the schema. Looks like due to complexity of the query. Other simple SPs are fine. Any idea? – DotNet Developer Mar 16 '22 at 14:14
  • Sorry, I don't know devtools well enough to tell you. If there is some way to manually tell it what the schema is then you could use that. One alternative might be to explicitly define the temp tables in the stored procedure and then `INSERT` into them instead of using `SELECT INTO`. SQL Server can usually determine the schema at compile time in those cases (but I don't know if that's what the wizard is using to infer the schema). – RBarryYoung Mar 16 '22 at 14:21
  • @RBarryYoung, I did as you instructed and tried many other workarounds. None of them helped. The problem is usage of temporary tables in the stored procedure. If temporary tables are used in stored procedure then VS Designer is unable to get schema. – DotNet Developer Mar 18 '22 at 02:27
  • Sorry, not sure what else you can try. You may want to ask this at the MSDN forums. – RBarryYoung Mar 18 '22 at 11:46
  • 1
    There are two possible answers at this thread: https://stackoverflow.com/questions/17368886/tableadapter-configuration-wizard-doesnt-like-temp-tables-in-sp. – RBarryYoung Mar 18 '22 at 11:53
  • 1
    This answer, you gave link to, solves the problem. – DotNet Developer Mar 18 '22 at 12:36

0 Answers0