Sqlserver中的一些技巧
2024-07-21 02:07:56
供稿:网友
获取一个sqlserver上的所有数据库信息 用存储过程 sp_databases
在查询分析其中执行命令exec sp_databases
结果:master 14464 null
model 1280 null
msdb 14336 null
northwind 4352 null
pubs 2560 null
store 1912 null
tempdb 8704 null
test 1272 null
获取一个数据库的所有表用存储过程 sp_tables
执行命令: use northwind exec sp_tables 结果:
northwind dbo sysusers system table null
northwind dbo categories table null
northwind dbo customercustomerdemo table null
northwind dbo customerdemographics table null
northwind dbo customers table null
northwind dbo dtproperties table null
northwind dbo employees table null
northwind dbo employeeterritories table null
(.......)
获取一个表的列信息用存储过程sp_columns
运行exec sp_columns 'orders' (orders为表名) 结果
northwind dbo orders orderid 4 int identity 10 4 0 10 0 null null 4 null null 1 no 56
northwind dbo orders customerid -8 nchar 5 10 null null 1 null null -8 null 10 2 yes 39
northwind dbo orders employeeid 4 int 10 4 0 10 1 null null 4 null null 3 yes 38
northwind dbo orders orderdate 11 datetime 23 16 3 null 1 null null 9 3 null 4 yes 111
northwind dbo orders requireddate 11 datetime 23 16 3 null 1 null null 9 3 null 5 yes 111
northwind dbo orders shippeddate 11 datetime 23 16 3 null 1 null null 9 3 null 6 yes 111
northwind dbo orders shipvia 4 int 10 4 0 10 1 null null 4 null null 7 yes 38
northwind dbo orders freight 3 money 19 21 4 10 1 null (0) 3 null null 8 yes 110
northwind dbo orders shipname -9 nvarchar 40 80 null null 1 null null -9 null 80 9 yes 39
(......)
获取一个数据库的所有存储过程,可以用
select * from sysobjects where type='p'
执行所得结果:
custordersdetail 789577851 p 1 0 1610612736 0 0 0 2000-08-06 01:34:52.513
custordersorders 805577908 p 1 0 1610612736 0 0 0 2000-08-06 01:34:52.733
custorderhist 821577965 p 1 0 1610612736 0 0 0 2000-08-06 01:34:52.967
salesbycategory 837578022 p 1 0 1610612736 0 0 0 2000-08-06 01:34:53.200
(......)
sysobjects这个东西还有其他一些用法,具体可参照sqlserver连机帮助
在ado.net里面获取一个存储过程的参数信息:
sqlconnection connect = new sqlconnection(connectionstring);
connect.open();
sqlcommand sc = new sqlcommand("salesbycategory", connect); // salesbycategory 为northwind数据库中的一个存储过程.
sc.commandtype = commandtype.storedprocedure;
sqlcommandbuilder.deriveparameters(sc);
foreach(sqlparameter param in sc.parameters)
{
console.writeline("name:{0}, size:{1}, type:{2}, value:{3},direction:{4}, isnull:{5}", param.parametername, param.size, param.dbtype, param.value, param.direction, param.isnullable);
}