动态SQL
Sample1:
/* Variable Declaration */
DECLARE @EmpID AS SMALLINT
DECLARE @SQLQuery AS NVARCHAR(500)
/* Build and Execute a Transact-SQL String with a single parameter value Using EXECUTE Command */
SET @EmpID = 1001
SET @SQLQuery = 'SELECT * from tblEmployees WHERE EmployeeID = ' + CAST(@EmpID AS NVARCHAR(10))
Print @SQLQuery
EXECUTE(@SQLQuery)
Sample2:
* Using sp_executesql - Example 1.1 */
/* Variable Declaration */
DECLARE @EmpID AS SMALLINT
DECLARE @SQLQuery AS NVARCHAR(500)
DECLARE @ParameterDefinition AS NVARCHAR(100)
/* Build and Execute a Transact-SQL String with a single parameter value Using sp_executesql Command */
SET @EmpID = 1001
SET @SQLQuery = 'SELECT * from tblEmployees WHERE EmployeeID = @EmpID'
/* Specify Parameter Format */
SET @ParameterDefinition = '@EmpID SMALLINT'
EXECUTE sp_executesql @SQLQuery, @ParameterDefinition, @EmpID
Sample3:
ALTER Procedure sp_EmployeeSelect
@EmployeeName NVarchar(100),
@Department NVarchar(50),
@Designation NVarchar(50),
@StartDate DateTime,
@EndDate DateTime,
@Salary Decimal(10,2),
@Total INT OUTPUT
AS
Set NoCount ON
Declare @SQLQuery AS NVarchar(4000)
Declare @ParamDefinition AS NVarchar(2000)
Set @SQLQuery = 'Select * from tblEmployees where (1=1) '
If @EmployeeName Is Not Null
Set @SQLQuery = @SQLQuery + ' And (EmployeeName = @EmployeeName)'
If @Department Is Not Null
Set @SQLQuery = @SQLQuery + ' And (Department = @Department)'
If @Designation Is Not Null
Set @SQLQuery = @SQLQuery + ' And (Designation = @Designation)'
If (@StartDate Is Not Null) AND (@EndDate Is Not Null)
Set @SQLQuery = @SQLQuery + ' And (JoiningDate BETWEEN @StartDa
相关文档:
课程介绍
SQL Server 2008是微软的最新数据库系统,其在原有SQL Server 2005的基础上,进行了重大的改进。本课程将从维护与开发两个方面进行较系统地讲述,使学员能够熟练使用SQL Server 2008开发强健的数据库应用。
适合对象
数据库管理或开发人员
课程长度
& ......
一、内部函数
1、内部合计函数
1)COUNT(*) 返回行数
2)COUNT(DISTINCT COLNAME)返回指定列中唯一值的个数
3)SUM(COLNAME/EXPRESSION)返回指定列或表达式的数值和;
4)SUM(DISTINCT COLNAME) 返回指定列中唯一值的和
5)AVG(COLNAME/EXPRESSION)返回指定列或表达式中的数值平均值
6)AV ......
(1) 选择最有效率的表名顺序(只在基于规则的优化器中有效):
ORACLE 的解析器按照从右到左的顺序处理from子句中的表名,from子句中写在最后的表(基础表 driving table)将被最先处理,在from子句中包含多个表的情况下,你必须选择记录条数最少的表作为基础表。如果有3个以上的表连接查询, 那就需要选择交叉表(intersectio ......