易截截图软件、单文件、免安装、纯绿色、仅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)                -- 成功
     


相关文档:

在sql server中关于游标cursor的使用

--此处使用pubs库
declare @myname varchar(50)
declare @fname varchar(20)
declare @lname varchar(20)
declare my_cursor cursor for
select fname,lname from employee order by emp_id
open my_cursor
fetch next from my_cursor into @fname,@lname
while @@fetch_status=0
begin
    set @ ......

SQL Server:定时作业的设置方法

【转】http://space.cnblogs.com/group/topic/33473/
http://www.cnblogs.com/ghd258/archive/2005/10/24/260836.html
如果在SQL Server 里需要定时或者每隔一段时间执行某个存储过程或3200字符以内的SQL语句时,可以用管理->SQL Server代理->作业来实现。 
  1、管理->SQL Server代 ......

SQL Table

Table-Naming Standards
Table-naming standards, as well as any standard within a business, are critical to
maintaining control. After studying the tables and data in the previous sections, you
probably noticed that each table’s suffix is _TBL. This is a naming standard selected
for use, suc ......

在SQL Server启动时自动执行存储过程

问题
  当sql server启动时,我很想运行一个存储过程。有没有一种方法可以在每次SQL Server服务启动时都会自动执行这个存储过程呢?
  专家解答
  sql Server提供了系统存储过程sp_procoption,这个存储过程可以用于当SQL Server服务启动时指派一个或者多个存储过程自动执行。这是一个很不错的选择,它可以用于多种 ......
© 2009 ej38.com All Rights Reserved. 关于E健网联系我们 | 站点地图 | 赣ICP备09004571号