SQL Server的一些技巧

來源:互聯網
上載者:User

擷取一個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);

}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.