SQL 对于有N多字段的表,踢出不需要的字段,自动频出剩余字段的SQL语句
2011-08-22 09:46
627 查看
--本例采用northwind数据库的Employees 表,拼出不包括title和firstName 两个字段的SQL 语句
--select * from Employees
declare @index int --默认循环起始值1
declare @ExceptName varchar(200)--要排除的字段名称
declare @indexString int--索引值
declare @InTable varchar(200) --要拼串的表
declare @sql varchar(600) --频出的sql语句
select @ExceptName='title,firstName',@index=1,@sql='select ',@InTable='Employees',@indexString=0 --初始化条件
begin
declare @tempStr nvarchar(800)
declare myCur cursor for ( select name from syscolumns where id=object_id(N''+@InTable+'') )
open myCur
fetch next from myCur into @tempStr
while @@fetch_status = 0
begin
set @tempStr=@tempStr
set @indexString=CHARINDEX(@tempStr, @ExceptName)
if(@indexString=0)
set @sql=@sql+@tempStr+','
fetch next from myCur into @tempStr
end
close myCur
deallocate myCur
end
SELECT @sql=SUBSTRING(@sql,1,(len(@sql)-1))
set @sql=@sql+' from '+@InTable
select @sql
--再判断两个字段 名称相同的问题上,还是有点问题的,再考虑
select EmployeeID,LastName,TitleOfCourtesy,BirthDate,HireDate,Address,City,Region,PostalCode,Country,HomePhone,Extension,Photo,Notes,ReportsTo,PhotoPath from Employees
--select * from Employees
declare @index int --默认循环起始值1
declare @ExceptName varchar(200)--要排除的字段名称
declare @indexString int--索引值
declare @InTable varchar(200) --要拼串的表
declare @sql varchar(600) --频出的sql语句
select @ExceptName='title,firstName',@index=1,@sql='select ',@InTable='Employees',@indexString=0 --初始化条件
begin
declare @tempStr nvarchar(800)
declare myCur cursor for ( select name from syscolumns where id=object_id(N''+@InTable+'') )
open myCur
fetch next from myCur into @tempStr
while @@fetch_status = 0
begin
set @tempStr=@tempStr
set @indexString=CHARINDEX(@tempStr, @ExceptName)
if(@indexString=0)
set @sql=@sql+@tempStr+','
fetch next from myCur into @tempStr
end
close myCur
deallocate myCur
end
SELECT @sql=SUBSTRING(@sql,1,(len(@sql)-1))
set @sql=@sql+' from '+@InTable
select @sql
--再判断两个字段 名称相同的问题上,还是有点问题的,再考虑
select EmployeeID,LastName,TitleOfCourtesy,BirthDate,HireDate,Address,City,Region,PostalCode,Country,HomePhone,Extension,Photo,Notes,ReportsTo,PhotoPath from Employees
相关文章推荐
- PL/SQL Developer中输入SQL语句时如何自动提示字段
- SQL语句对于多表联合查询重复字段的特殊语法处理
- sql语句自动匹配两个表两个字段内容相似的记录
- sqlserver uniqueidentifier 字段值自动填充语句
- NDatabase 入门,简单使用 增删改查。让NDatabase带你脱离ADO.net,各种SQL 语句,各种DBMS,各种CRM,IOC之类的烦恼。我们也不需要仓库设计模式了,你妹的。不要表了,不要设计数据库字段了。就这样!
- MyBatis拦截器动态修改SQL语句及参数值(场景:查询中字段值中特殊字符自动转义)
- 对于PowerDesigner生成sql语句中表和字段带引号的问题
- sql语句中对字段自动补足
- 对于PowerDesigner生成sql语句中表和字段带引号的问题
- PB中自动生成的sql语句,在comment不存在时,自动使用name字段作为注释的设置方法
- mybaites 通过字段自动生成sql 语句
- 取得SQL Server 2005各表、字段的说明等信息的SQL语句
- 一个字段匹配查询的SQL语句
- 如何获得Sybase中的数据库、表名、字段的SQL语句
- 一句简单的SQL语句,找出表中某个字段的重复值
- sql自动编号字段
- ACCESS中写SQL语句时尽量把表名和字段名用中括号括起来
- Sql语句更改字段类型及主键等实用整理
- 表字段出现oracle sql语句关键字时
- 页面刚开始加载时文本框显示1,2(从1自动后增)的sql语句