`
wsql
  • 浏览: 12125505 次
  • 性别: Icon_minigender_1
  • 来自: 深圳
文章分类
社区版块
存档分类
最新评论

How To Use Dynamic Sql in Sql Server ?

阅读更多

How To Use Dynamic Sql in Sql Server ?

动态SQL sql server Procedure中的应用

Create PROCEDURE [dbo].[Proc_Get_Serial_No]

(

@Table_Name varchar(20),

@No_File varchar(20)

)

AS

Declare @Serial_No int

Begin

Declare @Sql nvarchar(max)

Set @Sql ='select @Serial_No= isnull(Max('+@No_File+'),0)+1 From '+@Table_Name+''

Execute sp_executesql @Sql,

N'@Serial_No int output',

@Serial_No output

print @Serial_No

End

---Result---

Serial_No

-----------

1

注意:对于Intput 的参数需要用 '+@parameter+'

对于output 的参数则需要在 执行动态sql 的时间以定义参数的形式说明。

如上面的: N'@Serial_No int output',然后才是参数输出, @Serial_No output

--If Your want to output more the one value, your can reference the sql section as below

Note: The Output Parameter Define.

Create PROCEDURE [dbo].[Proc_Get_Serial_No]

(

@Table_Name varchar(20),

@No_File varchar(20)

)

AS

Declare @Serial_No int

Declare @x varchar(10)

Begin

Declare @Sql nvarchar(max)

Set @Sql ='select @x=''y'', @Serial_No= isnull(Max('+@No_File+'),0)+1 From '+@Table_Name+''

Execute sp_executesql @Sql,

N'@Serial_No int output,@x varchar(10) output',

@Serial_No output,

@x output

Select @Serial_No as Serial_No,@x as 'xx'

End

---Result---

Serial_No xx

----------- ----------

1 y

分享到:
评论

相关推荐

    Murach's SQL Server 2016 for Developers

    We’ll explain how this implemented in SQLServer is and demonstrates some use cases for it (for example, a time-travel application). Chapter 8, Tightening the Security, introduces three new security ...

    Securing SQL Server(Apress,2016)

    Protect your data from attack by using SQL Server technologies to implement a defense-in-depth strategy, performing threat analysis, and encrypting sensitive data as a last line of defense against ...

    SQL Server DMVs in Action(Manning,2011)

    SQL Server DMVs in Action shows you how to obtain, interpret, and act on the information captured by DMVs to keep your system in top shape. The over 100 code examples help you master DMVs and give you...

    微软内部资料-SQL性能优化2

    Before we look at how SQL Server uses and manages its memory, we need to ensure a full understanding of the more common memory related terms. The following definitions will help you understand how SQL...

    Oracle Database 12c PL-SQL programming

    Oracle Database 12c PL/SQL Programming explains how to retrieve and process data, write PL/SQL statements, execute effective queries, incorporate PHP and Java, and work with dynamic SQL. Code testing...

    微软内部资料-SQL性能优化3

    An intent lock indicates that SQL Server wants to acquire a shared (S) lock or exclusive (X) lock on some of the resources lower down in the hierarchy. For example, a shared intent lock placed at the ...

    Oracle Database 10g PL-SQL Programming

    - **Variables and Constants**: How to declare and use variables and constants in PL/SQL. - **Operators**: Overview of arithmetic, relational, logical, and concatenation operators. - **Control ...

    微软内部资料-SQL性能优化5

    In a nonclustered index, the leaf level contains each index key, plus a bookmark that tells SQL Server where to find the data row corresponding to the key in the index. A bookmark can take one of two ...

    IBM DB2 SQL Reference

    - **How To Use This Book**:提供了如何有效使用此书的建议,帮助读者快速定位所需信息。 - **How This Book is Structured**:概述了书籍的整体结构,便于读者了解章节分布。 - **How to Read the Syntax Diagrams...

    Professional ASP.NET 3.5 SP1 Edition: In C# and VB(part1)

    How to work with data from enterprise databases including SQL Server® * Ways to debug, package, and deploy ASP.NET applications, monitor their health and performance, and handle errors * How ...

    MyBatisCodeHelper-Pro.zip

    MyBatisCodeHelper-Pro插件免费版 Features Type safe sql support, plugin can recognize mybatis dynamic sql ...How to use view on https://github.com/gejun123456/MyBatisCodeHelper-Pro qqGroup:914051156

    C#学习的101个经典例子

    Windows Forms - Use Format Codes to Format Data in Strings Windows Forms - Use Regular Expressions Windows Forms - Use the Clipboard Windows Forms - XP Theme Support Windows Forms -- Owner Drawn Menus...

    Firebird Maestro 17.1 带破解

    Firebird server maintenance operations such as backup and restore database, shutdown database and bringing database online are easy and fast to perform in Firebird Maestro. Full customization ...

    beginning VisualBasic2010(英文版)入门必读

    The chapters walk you through the SQL SELECT statement, Access queries, data access components, and data binding.Chapters 17 and 18 provide an introduction to Dynamic Data Web Sites and ASP.NET and ...

    Troubleshooting Oracle Performance, 2nd Edition

    and executing SQL statements and how to instrument application code and database calls. It also introduces some important terms that are frequently used in the book. Part 2 explains how to approach ...

    11g_plsql_user_guide_and_reference.pdf

    The `CONTINUE` statement has been added to PL/SQL, allowing developers to skip the rest of the current iteration in a loop and proceed to the next one. This can be particularly useful for implementing...

Global site tag (gtag.js) - Google Analytics