Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Database & SysAdmin
  3. Database
  4. Is there a way to extract a value/result when you exec a sql string

Is there a way to extract a value/result when you exec a sql string

Scheduled Pinned Locked Moved Database
databasesql-serversysadmintutorialquestion
3 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • L Offline
    L Offline
    littleGreenDude
    wrote on last edited by
    #1

    SQL Server: Is there a way to extract a value/result when you exec a sql string? DECLARE @PaperCost money declare @PaperCategories nvarchar(30) set @PaperCategories = '3,7' DECLARE @sqlcmd varchar(255) select @sqlcmd = ' SELECT sum(amounts) FROM tbl_CostInfo where CatID in (' + RTRIM(@PaperCategories) + ') ' **SELECT @PaperCost = exec( @sqlcmd )** This sql block will reside inside of a stored proc, and the idea is to have a string passed in for the category values ('3,7' in this example) and return the corresponding cost. However, sql doesn't like the last line. Is there a way to extract the resulting value from an exec command?

    C A 2 Replies Last reply
    0
    • L littleGreenDude

      SQL Server: Is there a way to extract a value/result when you exec a sql string? DECLARE @PaperCost money declare @PaperCategories nvarchar(30) set @PaperCategories = '3,7' DECLARE @sqlcmd varchar(255) select @sqlcmd = ' SELECT sum(amounts) FROM tbl_CostInfo where CatID in (' + RTRIM(@PaperCategories) + ') ' **SELECT @PaperCost = exec( @sqlcmd )** This sql block will reside inside of a stored proc, and the idea is to have a string passed in for the category values ('3,7' in this example) and return the corresponding cost. However, sql doesn't like the last line. Is there a way to extract the resulting value from an exec command?

      C Offline
      C Offline
      Christian Graus
      wrote on last edited by
      #2

      Why not pass the categories as XML, use OpenXML to turn them into data, and do a normal query ?

      Christian Graus - Microsoft MVP - C++ "also I don't think "TranslateOneToTwoBillion OneHundredAndFortySevenMillion FourHundredAndEightyThreeThousand SixHundredAndFortySeven()" is a very good choice for a function name" - SpacixOne ( offering help to someone who really needed it ) ( spaces added for the benefit of people running at < 1280x1024 )

      1 Reply Last reply
      0
      • L littleGreenDude

        SQL Server: Is there a way to extract a value/result when you exec a sql string? DECLARE @PaperCost money declare @PaperCategories nvarchar(30) set @PaperCategories = '3,7' DECLARE @sqlcmd varchar(255) select @sqlcmd = ' SELECT sum(amounts) FROM tbl_CostInfo where CatID in (' + RTRIM(@PaperCategories) + ') ' **SELECT @PaperCost = exec( @sqlcmd )** This sql block will reside inside of a stored proc, and the idea is to have a string passed in for the category values ('3,7' in this example) and return the corresponding cost. However, sql doesn't like the last line. Is there a way to extract the resulting value from an exec command?

        A Offline
        A Offline
        andyharman
        wrote on last edited by
        #3

        You could try:

        create table #temp1 (
          SumAmounts money
          )
        set @SQL = 'SELECT sum(amounts) FROM tbl_CostInfo .....'
        exec(@SQL)
        select @PaperCost = SumAmounts from #temp1
        drop table #temp1
        

        However, a better way of acheiving this would be to use a UDF to turn your @PaperCategories string into a list of values. The resulting SQL would look a bit like:

        select @PaperCost = SUM(Amounts) from tbl_CostInfo
          where CatId in (
            select Element from dbo.udf_split(@PaperCategories))
        

        You should be able to find the source code for a similar UDF using google. Regards Andy

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups