初学 -- sql几点基础知识
2008-07-10 10:17
423 查看
1)查询某表主键
select column_name from information_schema.key_column_usage where table_name = '表名' and constraint_name like 'PK_%'
2)sql命令,像文本文件中写内容
declare @cmd sysname,@var sysname
set @var='文件的内容'
set @cmd='echo ' +@var+' >E://文件名.txt'
exec master..xp_cmdshell @cmd
3)sql事务
SET XACT_ABORT ON -- 执行失败自动回滚,用于存储过程
save tran point --保存回滚点
rollback tran point -- 回滚至回滚点
4)游标实例
declare @name varchar(20)
declare mycursor cursor
for
select categoryname from categories
open mycursor
fetch mycursor into @name
while @@fetch_status<>-1
if @@fetch_status <>-2
begin
print @name
fetch mycursor into @name
end
close mycursor
deallocate mycursor
5)sp_executesql 执行sql语句
create proc a
@tableName varchar(20),
@RowCount int output
as
declare @rowcountstring nvarchar(500)
set @rowcountstring='select @RowCount = count(*) from '+@tableName
exec sp_executesql @rowcountstring,N'@RowCount int output',@RowCount output
go
declare @count int
exec a 'products',@count output
print @count
select column_name from information_schema.key_column_usage where table_name = '表名' and constraint_name like 'PK_%'
2)sql命令,像文本文件中写内容
declare @cmd sysname,@var sysname
set @var='文件的内容'
set @cmd='echo ' +@var+' >E://文件名.txt'
exec master..xp_cmdshell @cmd
3)sql事务
SET XACT_ABORT ON -- 执行失败自动回滚,用于存储过程
save tran point --保存回滚点
rollback tran point -- 回滚至回滚点
4)游标实例
declare @name varchar(20)
declare mycursor cursor
for
select categoryname from categories
open mycursor
fetch mycursor into @name
while @@fetch_status<>-1
if @@fetch_status <>-2
begin
print @name
fetch mycursor into @name
end
close mycursor
deallocate mycursor
5)sp_executesql 执行sql语句
create proc a
@tableName varchar(20),
@RowCount int output
as
declare @rowcountstring nvarchar(500)
set @rowcountstring='select @RowCount = count(*) from '+@tableName
exec sp_executesql @rowcountstring,N'@RowCount int output',@RowCount output
go
declare @count int
exec a 'products',@count output
print @count
相关文章推荐
- 联接基础知识---SQL中
- 数据库基础与SQL语言基础知识整理
- SQL Server 的SQL基础知识
- ABAP-SQL基础知识
- sql基础知识-----主外键、参照完整性
- python初学基础知识一
- SQL基础知识--多行查询结果拼接
- 知识整理:sql基础
- sql-server数据库基础知识五(用户、角色、权限)
- SQL 基础知识 -- identify seed overflow
- SQL基础知识归纳总结
- SQL 基础知识-声明变量-变量类型
- 测试一下你的T-SQL基础知识-subquery
- Oracle11g基础知识2【SQL学习笔记】
- sql基础知识
- mysql--SQL编程(基础知识) 学习笔记1
- SQL - 基础知识(1)
- 对SQL语句的基础知识总结三(SQL语句的进阶)
- PL/SQL基础知识讲解
- PL/SQL - 基础知识(1)