Dynamic statement SQL statement notation

Source: Internet
Author: User

  1. /************************************************************************************************************** ****************************************
  2. Dynamic statement syntax: Exec/sp_executesql syntax
  3. Finishing Man: China Wind (Roy)
  4. Date: 2008.06.06
  5. *************************************************************************************************************** ***************************************/
  6. Dynamic statement Syntax:
  7. --Method 1 query table changed to dynamic
  8. SELECT * from sysobjects
  9. EXEC (' select id,name from sysobjects ')
  10. EXEC sp_executesql n' select id,name from sysobjects '-one more n for Unicode
  11. --Method 2: Field name, table name, database name, etc. as variables, use dynamic SQL
  12. DECLARE @FName varchar (20)
  13. Set @FName =' ID '
  14. EXEC (' select ' [email protected]+' from sysobjects where ' [email protected]+' =5 ')
  15. DECLARE @s varchar (1000)
  16. Set @s=n' SELECT ' [email protected]+' from sysobjects where ' [email protected]+' =5 '
  17. EXEC sp_executesql @s--will error
  18. declare @s nvarchar (1000)--Change to nvarchar
  19. Set @s=n' SELECT ' [email protected]+' from sysobjects where ' [email protected]+' =5 '
  20. EXEC sp_executesql @s--Success
  21. --Method 3: Input parameters
  22. DECLARE @i int,@s nvarchar (+)
  23. Set @i=5
  24. EXEC (' select Id,name from sysobjects where id= ' [email protected])
  25. Set @s=' select Id,name from sysobjects where [email protected] '
  26. EXEC sp_executesql @s,n' @i int ', @i--here input parameter to add N
  27. --Method 4: Output parameters
  28. DECLARE @i int,@s nvarchar (+)
  29. Set @s=' select @i=count (1) from sysobjects '
  30. --with Exec
  31. EXEC (' Declare @i int ' [Email protected]+' Select @i ')--add the entire statement with a string to execute.
  32. --With sp_executesql
  33. EXEC sp_executesql @s,n' @i int output ', @i output--here output parameter to add N
  34. Select @i
  35. --Method 5: Input and output
  36. --With sp_executesql
  37. DECLARE @i int, @con int,@s nvarchar (+)
  38. Set @i=5
  39. Select @s=' Select @con =count (1) from sysobjects where id>@i '
  40. EXEC sp_executesql @s,n' @con int output,@i int ', @con output, @i
  41. Select @con
  42. --with Exec
  43. DECLARE @i int,@s nvarchar (+)
  44. Set @i=5
  45. Select @s=' declare @con int select @con =count (1) from sysobjects where id> ' +rtrim (@i) +' select @con '
  46. EXEC (@s)

http://blog.csdn.net/roy_88/article/details/3020586

Dynamic statement SQL statement notation

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.