1,获得所有数据库名的方法:
select name from master..Sysdatabases order by name
2,获得指定数据库的表名
select name from TestDB..Sysobjects where XType='U' order by name
XType='U':表示所有用户表。
XType='S' 表示所有系统表。
3,获取所有字段名
select name from TestDB..SysColumns where id=Object_id('TableName')
4,用sql排除重复结果中指定字段最大值的记录(pid 相同的记录中,id最大的那条)
select × from table a where id not exists (select 1 from table where pid = a.pid and id >a.id)
select * from table a join(select pid,max(id) as max_id from table group by pid) b on a.pid = b.pid and a.id = b.max_id
5,自定义方法 分组中的多行数据中指定字段合并成一行一列的值。
create function [dbo].moryToOne( @pid varchar(10))
returns nvarchar(1000)
as
begin
declare @a varchar(1000)
set @a=''
select @a = @a +username from TestTable where pid = @pid
return @a
end
6.存储过程的更新语句的格式
Begin TRY
Begin tran
---更新
commit Tran
end Try
begin catch
set @retrunMsg = error_message();
end catch
在存储过程中对变量赋值的有set 和select :
declare @sql varchar(40)
set @sql = 'select getdate() as todate'
exec(@sql)
go
declare @sql varchar(40)
select @sql ='select getdate() as todate'
exec(@sql)
insert students select 1,'tom' unoin all
select 2,'sam'