Upload
alexander-tucker
View
215
Download
3
Embed Size (px)
Citation preview
SQL Server
اسکریپت درج مقدار در جدول
USE Accounting; DECLARE @Ident int; INSERT INTO Orders (CustomerNo,OrderDate, EmployeeID) VALUES (gETDATE,1); SELECT @Ident = @@IDENTITY; INSERT INTO OrderDetails (OrderID, PartNo, Description, UnitPrice, Qty) VALUES (@Ident, ‘2R2416’, ‘Cylinder Head’, 1300, 2); SELECT ‘The OrderID of the INSERTed row is ‘ +
CONVERT(varchar(8),@Ident);
SETاستفاده از
USE AdventureWorks2008; DECLARE @Test money; SET @Test = (SELECT MAX)UnitPrice)
FROM Sales.SalesOrderDetail) SELECT @Test;
استفاده SELECTبرای انتخاب از USE AdventureWorks2008; DECLARE @Test money; SELECT @Test = MAX)UnitPrice)
FROM Sales.SalesOrderDetail; SELECT @Test;
SQL Server چند متغیر سیستمی @@ERROR @@IDENTITY @@REMSERVER @@ROWCOUNT @@SERVERNAME @@TRANCOUNT @@VERSION
USE AdventureWorks2008; GO DECLARE @RCount int; SELECT * FROM Person.Person; SELECT @RCount =
@@ROWCOUNT;
Batch USE AdventureWorks2008; DECLARE @MyVarchar varchar)50); --This DECLARE only lasts for this batch! SELECT @MyVarchar = ‘Honey, I’’m home...’; PRINT ‘Done with first Batch...’; GO PRINT @MyVarchar; --This generates an error since @MyVarchar --isn’t declared in this batch PRINT ‘Done with second Batch’; GO PRINT ‘Done with third batch’; -- Notice that this still gets executed -- even after the error GO
نتیجه اجرای اسکریپت قبل Done with first Batch... Msg 137, Level 15, State 2, Line 2 Must declare the scalar variable
“@MyVarchar”. Done with third batch
دستوراتی که باید درBatchجداگانه نوشته شوند
CREATE DEFAULT CREATE PROCEDURE CREATE RULE CREATE TRIGGER CREATE VIEW
USE Test; SELECT TABLE_CATALOG FROM
INFORMATION_SCHEMA.TABLES WHERE TABLE_NAME = ‘TestTable’;
Command از طریق SQL اجرای دستورات
C:\>sqlcmd -Usa -Pmypass -i testsql.sql [ -d <db name> ] [ -o <output file> ]
SQLCMD -Usa -Pmypass –Q “SELECT
* FROM AdventureWorks2008.Production.Location”
EXECنکات استفاده از runs under a separate scope than code that
calls it By default, it runs under the same security
context as the current user It runs under the same connection and
transaction context as the calling object Concatenation that requires a function call
must be performed on the EXEC string prior to actually
calling the EXEC statement EXEC NOT Availabe inside a user-defined
function.
SQL روش دیگر اجرای دستورات sp_executesql.
WAITFORدستور
WAITFOR DELAY ‘01:00’;
WAITFOR TIME ‘10:00’;
IF NOT EXISTS ( SELECT s.name AS SchemaName, t.name AS
TableName FROM sys.schemas s JOIN sys.tables t ON s.schema_id = t.schema_id WHERE s.name = ‘dbo’ AND t.name = ‘OurIFTest’)
CREATE TABLE OurIFTest( Col1 int PRIMARY KEY); SELECT ‘Found Table ‘ + s.name + ‘.’ + t.name FROM sys.schemas s JOIN sys.tables t ON s.schema_id = t.schema_id WHERE s.name = ‘dbo’ AND t.name = ‘OurIFTest’;
Use AdventureWorks2008; GO SELECT TOP 10 SalesOrderID, SalesOrderID % 10 AS ‘Last Digit’, Position = CASE SalesOrderID % 10
WHEN 1 THEN ‘First’ WHEN 2 THEN ‘Second’ WHEN 3 THEN ‘Third’ WHEN 4 THEN ‘Fourth’ ELSE ‘Something Else’
END FROM Sales.SalesOrderHeader;
SELECT TOP 10 SalesOrderID % 10 AS ‘OrderLastDigit’, ProductID % 10 AS ‘ProductLastDigit’,“How Close?” = CASE WHEN (SalesOrderID % 10) < 3 THEN ‘Ends With Less
Than Three’ WHEN ProductID = 6 THEN ‘ProductID is 6’ WHEN ABS(SalesOrderID % 10 - ProductID) <= 1 THEN
‘Within 1’ ELSE ‘More Than One Apart’ END FROM Sales.SalesOrderDetail ORDER BY SalesOrderID DESC;
مهار خطاها در اسکریپت
BEGIN TRY { <sql statement(s)> } END TRY BEGIN CATCH { <sql statement(s)> } END CATCH [ ; ]
BEGIN TRY CREATE TABLE OurIFTest(Col1 int PRIMARY KEY); END TRY BEGIN CATCH DECLARE @ErrorNo int, @Severity tinyint,@State smallint, @LineNo int,@Message nvarchar(4000); SELECT @ErrorNo = ERROR_NUMBER(), @Severity = ERROR_SEVERITY(), @State = ERROR_STATE(),@LineNo = ERROR_LINE (), @Message = ERROR_MESSAGE(); IF @ErrorNo = 2714 PRINT ‘WARNING: Skipping CREATE as table already exists’; ELSE RAISERROR(@Message, 16, 1 ); END CATCH