how to get sp_executesql result into a temp table variable
  • Hi today i am sharing how can we get EXEC sp_ExecuteSql @Sql result in temp table.

    Create Table

    CREATE table #tempTable(
    Date datetime, SalesInvoiceNo varchar(15), ProductId int, ProductName varchar(100), Quantity decimal(14,4)
    , Price decimal(14,2), Amount decimal(18,4)
    , BrandId int, BrandName varchar(50), Code varchar(15), CustomerName varchar(100)

    Set Query in Sql Variable

    declare @Sql nvarchar(max)

    set @Sql='insert into #tempTable  


            Date, SalesInvoiceNo, SalesDetail.ProductId, ProductName, Quantity, Price, Amount
            , ProductBrands.BrandId, ProductBrands.BrandName, Customer.Code, Customer.Name CustomerName

            from SalesMaster
            Inner Join SalesDetail On SalesMaster.SalesMasterId=SalesDetail.SalesMasterId
            Inner Join Product on SalesDetail.ProductId=Product.ProductId
            Inner Join ProductBrands on Product.BrandId=ProductBrands.BrandId
            Inner Join Customer on SalesMaster.CustomerCode=Customer.Code'
    Execute Query
            EXEC sp_ExecuteSql @Sql
     Fetch result from Temp Table variable

            SELECT * FROM #tempTable
            drop table #tempTable

Howdy, Stranger!

It looks like you're new here. If you want to get involved, or you want to Ask a new Question, Please Login or Create a new Account by Clicking below

Login with Facebook


Popular Posts of the Week