Download presentation
Presentation is loading. Please wait.
Published byAlexander Tucker Modified over 9 years ago
1
SQL Server
2
اسکریپت درج مقدار در جدول 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);
3
SET استفاده از USE AdventureWorks2008; DECLARE @Test money; SET @Test = (SELECT MAX)UnitPrice) FROM Sales.SalesOrderDetail) SELECT @Test;
4
برای انتخاب SELECT استفاده از USE AdventureWorks2008; DECLARE @Test money; SELECT @Test = MAX)UnitPrice) FROM Sales.SalesOrderDetail; SELECT @Test;
5
SQL Server چند متغیر سیستمی @@ERROR @@IDENTITY @@REMSERVER @@ROWCOUNT @@SERVERNAME @@TRANCOUNT @@VERSION
6
USE AdventureWorks2008; GO DECLARE @RCount int; SELECT * FROM Person.Person; SELECT @RCount = @@ROWCOUNT;
7
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
8
نتیجه اجرای اسکریپت قبل Done with first Batch... Msg 137, Level 15, State 2, Line 2 Must declare the scalar variable “@MyVarchar”. Done with third batch
9
جداگانه نوشته شوند Batch دستوراتی که باید در CREATE DEFAULT CREATE PROCEDURE CREATE RULE CREATE TRIGGER CREATE VIEW
10
USE Test; SELECT TABLE_CATALOG FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_NAME = ‘TestTable’;
11
Command از طریق SQL اجرای دستورات C:\>sqlcmd -Usa -Pmypass -i testsql.sql [ -d ] [ -o ] SQLCMD -Usa -Pmypass –Q “SELECT * FROM AdventureWorks2008.Production.Locati on”
12
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.
13
SQL روش دیگر اجرای دستورات sp_executesql.
14
WAITFOR دستور WAITFOR DELAY ‘01:00’; WAITFOR TIME ‘10:00’;
15
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’;
16
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;
17
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;
18
مهار خطاها در اسکریپت BEGIN TRY { } END TRY BEGIN CATCH { } END CATCH [ ; ]
19
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
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.