易截截图软件、单文件、免安装、纯绿色、仅160KB

动态SQL语句

动态语句
1 :普通SQL语句可以用exec执行
Select * from tableName
exec('select * from tableName')
exec sp_executesql N'select * from tableName' -- 请注意字符串前一定要加N
2:字段名,表名,数据库名之类作为变量时,必须用动态SQL
declare @fname varchar(20)
set @fname = 'FiledName'
Select @fname from tableName -- 错误,不会提示错误,但结果为固定值FiledName,并非所要。
exec('select ' + @fname + ' from tableName') -- 请注意 加号前后的 单引号的边上加空格
当然将字符串改成变量的形式也可
declare @fname varchar(20)
set @fname = 'FiledName' --设置字段名
declare @s varchar(1000)
set @s = 'select ' + @fname + ' from tableName'
exec(@s) -- 成功
exec sp_executesql @s -- 此句会报错
declare @s Nvarchar(1000) -- 注意此处改为nvarchar(1000)
set @s = 'select ' + @fname + ' from tableName'
exec(@s) -- 成功
exec sp_executesql @s -- 此句正确
3. 输出参数
declare @num int, @sqls nvarchar(4000)
set @sqls='select count(*) from tableName'
exec(@sqls)
--如何将exec执行结果放入变量中?
declare @num int, @sqls nvarchar(4000)
set @sqls='select @a=count(*) from tableName '
exec sp_executesql @sqls,N'@a int output',@num output
select @num
1 :普通SQL语句可以用Exec执行      例:      Select * from tableName
                Exec('select * from tableName')
                Exec sp_executesql N'select * from tableName'    -- 请注意字符串前一定要加N
2:字段名,表名,数据库名之类作为变量时,必须用动态SQL
    错误:       declare @fname varchar(20)
                set @fname = 'FiledName'
                Select @fname from tableName   


相关文档:

菜鸟学SQL语句

为了大家更容易理解我举出的SQL语句,本文假定已经建立了一个学生成绩管理数据库,全文均以学生成绩的管理为例来描述。
  1.在查询结果中显示列名:
  a.用as关键字:select name as '姓名' from students order by age
  b.直接表示:select name '姓名' from students order by age
  2.精确查找:
  a.用 ......

sql 查询慢的48个原因分析。

查询速度慢的原因很多,常见如下几种:
  1、没有索引或者没有用到索引(这是查询慢最常见的问题,是程序设计的缺陷)
  2、I/O吞吐量小,形成了瓶颈效应。
  3、没有创建计算列导致查询不优化。
  4、内存不足
  5、网络速度慢
  6、查询出的数据量过大(可以采用多次查询,其他的方法降低数据量)
  7、 ......

SQL数据库连接语句

一般的远程访问的写成这样:
Data Source=IP;Initial Catalog=数据库名;UserID=用户名;Password=密码
本地访问的写成这样:
Data Source=(local);Initial Catalog=数据库名;UserID=用户名;Password=密码
如果是本地的,通过windows组件验证的(也就是没有用户名,密码的)写成这样:
Data Source=(local);Initial Cata ......

规划院培训费用统计查询的sql语句

create or replace view pxfy_view as
select t1.form_id,t1.reserve as mid,t1.feiyong,t.cjpxr_name as full_name,1 as pxlx,t1.topic,t1.identity,
     t1.begin_time as CREATEDATE,t1.end_time as completedate,pv.city,pv.DEPT_ID
     from wf_oa_pxnb_cjry t,WF_OA_PX ......
© 2009 ej38.com All Rights Reserved. 关于E健网联系我们 | 站点地图 | 赣ICP备09004571号