Tables Name Using Variables at SP

Hi All,

I have SP like below :

USE [GPI_TRADING]
GO

/****** Object:  StoredProcedure [dbo].[ERV_SP_Update_Empty_Tax_No]    Script Date: 4/19/2018 4:06:51 AM ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO


-- =============================================
-- Author:            <Author,,Name>
-- Create date: <Create Date,,>
-- Description:      <Description,,>
-- =============================================

CREATE PROCEDURE [dbo].[ERV_SP_Delete_Stock_Detail_Roll]
      -- Add the parameters for the stored procedure here
      @NoTransaksi AS VARCHAR(30) = ''
      , @IDStock AS BIGINT = 0
        , @Year CHAR(4) = ''
      

AS
BEGIN
      -- SET NOCOUNT ON added to prevent extra result sets from
      -- interfering with SELECT statements.
      SET NOCOUNT ON;

    -- Insert statements for procedure here
 
 DELETE FROM TMSTOKBARANGDETIL2018
 WHERE NoTransaksi = @NoTransaksi
 AND IDStock = @IDStock

END

I want to use variable for TMSTOKBARANGDETIL2018 into TMSTOKBARANGDETIL@Year

How could I do this ?

Thank you.
LVL 1
emi_sastraAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Aneesh RetnakaranDatabase AdministratorCommented:
BEGIN
      -- SET NOCOUNT ON added to prevent extra result sets from
      -- interfering with SELECT statements.
      SET NOCOUNT ON;
declare @sql nvarchar(1000) ;
DECLARE @ParmDefinition NVARCHAR(500) = ' @NoTransaksi AS VARCHAR(30), @IDStock bigint'

    -- Insert statements for procedure here
 
SELECT @SQL  =  ' DELETE FROM TMSTOKBARANGDETIL'+@Year
 WHERE NoTransaksi = @NoTransaksi
 AND IDStock = @IDStock'

exec sp_executeSQL =@sql, @ParmDefinition, @NoTransaksi, @IDStock



END
Aneesh RetnakaranDatabase AdministratorCommented:
sorry i missed a quote

CREATE PROCEDURE dbo.ERV_SP_Delete_Stock_Detail_Roll
    -- Add the parameters for the stored procedure here
    @NoTransaksi AS VARCHAR (30) = '',
    @IDStock AS BIGINT = 0,
    @Year CHAR (4) = ''
AS
BEGIN
    -- SET NOCOUNT ON added to prevent extra result sets from
    -- interfering with SELECT statements.
    SET NOCOUNT ON;
    DECLARE @sql NVARCHAR (1000);
    DECLARE @ParmDefinition NVARCHAR (500) = ' @NoTransaksi AS VARCHAR(30), @IDStock bigint';

    -- Insert statements for procedure here

    SELECT @sql = ' DELETE FROM TMSTOKBARANGDETIL' + @Year + ' WHERE NoTransaksi = @NoTransaksi AND IDStock = @IDStock';

    EXEC sys.sp_executesql @sql, @ParmDefinition, @NoTransaksi, @IDStock;

END;

Open in new window

emi_sastraAuthor Commented:
Hi Aneesh,

-   DECLARE @ParmDefinition NVARCHAR (500) = ' @NoTransaksi AS VARCHAR(30), @IDStock bigint';
What this is for ?

Thank you.
Determine the Perfect Price for Your IT Services

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden with our free interactive tool and use it to determine the right price for your IT services. Download your free eBook now!

Aneesh RetnakaranDatabase AdministratorCommented:
that declares and stores the paramater definition thats used within the sp_executeSQL statement. If you check the documentation for sp_executeSQL you can get more info
emi_sastraAuthor Commented:
I try below code :

BEGIN
      -- SET NOCOUNT ON added to prevent extra result sets from
      -- interfering with SELECT statements.
      SET NOCOUNT ON;
       DECLARE @sql NVARCHAR (MAX);
       DECLARE @ParmDefinition NVARCHAR (500) = ' @NoTransaksi AS VARCHAR(30), @@ID BIGINT';

    -- Insert statements for procedure here

        SELECT @sql = ' DELETE FROM TMSTOKBARANGDETIL' + @Year + ' WHERE NoTransaksi = @NoTransaksi '
                      + ' AND IDStock = ( SELECT IDStock FROM TDSTOCKINOUTSERIALNO WHERE ID = @ID )';

    EXEC sys.sp_executesql @sql, @ParmDefinition, @NoTransaksi, @ID;
 
   SELECT @sql = ' DELETE FROM TDSTOCKINOUTSERIALNO' + @Year + ' WHERE ID = @ID  ';
              
    EXEC sys.sp_executesql @sql, @ParmDefinition, @NoTransaksi, @ID;
 
 
END

Is there something wrong here ?

Thank you.
Aneesh RetnakaranDatabase AdministratorCommented:
You defined @ID as @@ID,

You can print @sql before you execute; that way you can check for any sorts of errors in the statement

    -- interfering with SELECT statements.
    SET NOCOUNT ON;
    DECLARE @sql NVARCHAR (MAX);
    DECLARE @ParmDefinition NVARCHAR (500) = ' @NoTransaksi AS VARCHAR(30), @ID BIGINT';

    -- Insert statements for procedure here

    SELECT @sql = ' DELETE FROM TMSTOKBARANGDETIL' + @Year + ' WHERE NoTransaksi = @NoTransaksi ' + ' AND IDStock = ( SELECT IDStock FROM TDSTOCKINOUTSERIALNO WHERE ID = @ID )';
      PRINT  @sql
   -- EXEC sys.sp_executesql @sql, @ParmDefinition, @NoTransaksi, @ID;

    SELECT @sql = ' DELETE FROM TDSTOCKINOUTSERIALNO' + @Year + ' WHERE ID = @ID  ';
      PRINT @sql
    --EXEC sys.sp_executesql @sql, @ParmDefinition, @NoTransaksi, @ID;

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
emi_sastraAuthor Commented:
Hi Aneesh,

Great.

Thank you very much for your help.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft SQL Server

From novice to tech pro — start learning today.