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
分享到:
相关推荐
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 ...
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 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...
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 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...
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 ...
- **Variables and Constants**: How to declare and use variables and constants in PL/SQL. - **Operators**: Overview of arithmetic, relational, logical, and concatenation operators. - **Control ...
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 ...
- **How To Use This Book**:提供了如何有效使用此书的建议,帮助读者快速定位所需信息。 - **How This Book is Structured**:概述了书籍的整体结构,便于读者了解章节分布。 - **How to Read the Syntax Diagrams...
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插件免费版 Features Type safe sql support, plugin can recognize mybatis dynamic sql ...How to use view on https://github.com/gejun123456/MyBatisCodeHelper-Pro qqGroup:914051156
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 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 ...
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 ...
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 ...
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...