How to use dynamic sentence (exec/sp_executesql)

Source: Internet
Author: User
  1. -- Dynamic statement syntax
  2. /*************************************** **************************************** **************************************** *******************************
  3. Dynamic statement Syntax: exec/sp_executesql syntax
  4. Organized by: Roy)
  5. Date: 2008.06.06
  6. **************************************** **************************************** **************************************** ******************************/
  7. Dynamic statement Syntax:
  8. -- Method 1: Change the query table to dynamic
  9. Select * From sysobjects
  10. Exec ('select ID, name from sysobjects ')
  11. Exec sp_executesql n 'select ID, name from sysobjects '-- an additional N is Unicode
  12. -- Method 2: when the field name, table name, and database name are used as variables, dynamic SQL is used.
  13. Declare @ fname varchar (20)
  14. Set @ fname = 'id'
  15. Exec ('select' + @ fname + 'from sysobjects where' + @ fname + '= 5 ')
  16. Declare @ s varchar (1000)
  17. Set @ s = n'select' + @ fname + 'from sysobjects where' + @ fname + '= 5'
  18. Exec sp_executesql @ s -- an error is reported.
  19. Declare @ s nvarchar (1000) -- change to nvarchar
  20. Set @ s = n'select' + @ fname + 'from sysobjects where' + @ fname + '= 5'
  21. Exec sp_executesql @ s -- successful
  22. -- Method 3: input parameters
  23. Declare @ I int, @ s nvarchar (1000)
  24. Set @ I = 5
  25. Exec ('select ID, name from sysobjects where id = '+ @ I)
  26. Set @ s = 'select ID, name from sysobjects where id = @ I'
  27. Exec sp_executesql @ s, n' @ I int ', @ I -- N is added to the input parameter here.
  28. -- Method 4: Output Parameters
  29. Declare @ I int, @ s nvarchar (1000)
  30. Set @ s = 'select @ I = count (1) From sysobjects'
  31. -- Exec
  32. Exec ('Clare @ I int' + @ s + 'select @ I ') -- adds the entire statement to the string for execution.
  33. -- Use sp_executesql
  34. Exec sp_executesql @ s, n' @ I int output', @ I output -- N must be added to the output parameter here.
  35. Select @ I
  36. -- Method 5: Input and Output
  37. -- Use sp_executesql
  38. Declare @ I int, @ con int, @ s nvarchar (1000)
  39. Set @ I = 5
  40. Select @ s = 'select @ con = count (1) From sysobjects where ID> @ I'
  41. Exec sp_executesql @ s, n' @ con int output, @ I int ', @ con output, @ I
  42. Select @ con
  43. -- Exec
  44. Declare @ I int, @ s nvarchar (1000)
  45. Set @ I = 5
  46. Select @ s = 'Clare @ con int select @ con = count (1) From sysobjects where ID> '+ rtrim (@ I) + 'select @ con'
  47. Exec (@ s)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.