19
SQL Server

SQL Server. اسکریپت درج مقدار در جدول USE Accounting; DECLARE @Ident int; INSERT INTO Orders (CustomerNo,OrderDate, EmployeeID) VALUES (gETDATE,1); SELECT

Embed Size (px)

Citation preview

Page 1: SQL Server. اسکریپت درج مقدار در جدول USE Accounting; DECLARE @Ident int; INSERT INTO Orders (CustomerNo,OrderDate, EmployeeID) VALUES (gETDATE,1); SELECT

SQL Server

Page 2: SQL Server. اسکریپت درج مقدار در جدول USE Accounting; DECLARE @Ident int; INSERT INTO Orders (CustomerNo,OrderDate, EmployeeID) VALUES (gETDATE,1); SELECT

اسکریپت درج مقدار در جدول

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);

Page 3: SQL Server. اسکریپت درج مقدار در جدول USE Accounting; DECLARE @Ident int; INSERT INTO Orders (CustomerNo,OrderDate, EmployeeID) VALUES (gETDATE,1); SELECT

SETاستفاده از

USE AdventureWorks2008; DECLARE @Test money; SET @Test = (SELECT MAX)UnitPrice)

FROM Sales.SalesOrderDetail) SELECT @Test;

Page 4: SQL Server. اسکریپت درج مقدار در جدول USE Accounting; DECLARE @Ident int; INSERT INTO Orders (CustomerNo,OrderDate, EmployeeID) VALUES (gETDATE,1); SELECT

استفاده SELECTبرای انتخاب از USE AdventureWorks2008; DECLARE @Test money; SELECT @Test = MAX)UnitPrice)

FROM Sales.SalesOrderDetail; SELECT @Test;

Page 5: SQL Server. اسکریپت درج مقدار در جدول USE Accounting; DECLARE @Ident int; INSERT INTO Orders (CustomerNo,OrderDate, EmployeeID) VALUES (gETDATE,1); SELECT

SQL Server چند متغیر سیستمی @@ERROR @@IDENTITY @@REMSERVER @@ROWCOUNT @@SERVERNAME @@TRANCOUNT @@VERSION

Page 6: SQL Server. اسکریپت درج مقدار در جدول USE Accounting; DECLARE @Ident int; INSERT INTO Orders (CustomerNo,OrderDate, EmployeeID) VALUES (gETDATE,1); SELECT

USE AdventureWorks2008; GO DECLARE @RCount int; SELECT * FROM Person.Person;   SELECT @RCount =

@@ROWCOUNT;

Page 7: SQL Server. اسکریپت درج مقدار در جدول USE Accounting; DECLARE @Ident int; INSERT INTO Orders (CustomerNo,OrderDate, EmployeeID) VALUES (gETDATE,1); SELECT

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

Page 8: SQL Server. اسکریپت درج مقدار در جدول USE Accounting; DECLARE @Ident int; INSERT INTO Orders (CustomerNo,OrderDate, EmployeeID) VALUES (gETDATE,1); SELECT

نتیجه اجرای اسکریپت قبل Done with first Batch... Msg 137, Level 15, State 2, Line 2 Must declare the scalar variable

“@MyVarchar”. Done with third batch

Page 9: SQL Server. اسکریپت درج مقدار در جدول USE Accounting; DECLARE @Ident int; INSERT INTO Orders (CustomerNo,OrderDate, EmployeeID) VALUES (gETDATE,1); SELECT

دستوراتی که باید درBatchجداگانه نوشته شوند

CREATE DEFAULT CREATE PROCEDURE CREATE RULE CREATE TRIGGER CREATE VIEW

Page 10: SQL Server. اسکریپت درج مقدار در جدول USE Accounting; DECLARE @Ident int; INSERT INTO Orders (CustomerNo,OrderDate, EmployeeID) VALUES (gETDATE,1); SELECT

USE Test; SELECT TABLE_CATALOG FROM

INFORMATION_SCHEMA.TABLES WHERE TABLE_NAME = ‘TestTable’;

Page 11: SQL Server. اسکریپت درج مقدار در جدول USE Accounting; DECLARE @Ident int; INSERT INTO Orders (CustomerNo,OrderDate, EmployeeID) VALUES (gETDATE,1); SELECT

Command از طریق SQL اجرای دستورات

C:\>sqlcmd -Usa -Pmypass -i testsql.sql [ -d <db name> ] [ -o <output file> ]

  SQLCMD -Usa -Pmypass –Q “SELECT

* FROM AdventureWorks2008.Production.Location”

Page 12: SQL Server. اسکریپت درج مقدار در جدول USE Accounting; DECLARE @Ident int; INSERT INTO Orders (CustomerNo,OrderDate, EmployeeID) VALUES (gETDATE,1); SELECT

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.

Page 13: SQL Server. اسکریپت درج مقدار در جدول USE Accounting; DECLARE @Ident int; INSERT INTO Orders (CustomerNo,OrderDate, EmployeeID) VALUES (gETDATE,1); SELECT

SQL روش دیگر اجرای دستورات sp_executesql.

Page 14: SQL Server. اسکریپت درج مقدار در جدول USE Accounting; DECLARE @Ident int; INSERT INTO Orders (CustomerNo,OrderDate, EmployeeID) VALUES (gETDATE,1); SELECT

WAITFORدستور

WAITFOR DELAY ‘01:00’;

WAITFOR TIME ‘10:00’;

Page 15: SQL Server. اسکریپت درج مقدار در جدول USE Accounting; DECLARE @Ident int; INSERT INTO Orders (CustomerNo,OrderDate, EmployeeID) VALUES (gETDATE,1); SELECT

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’;

Page 16: SQL Server. اسکریپت درج مقدار در جدول USE Accounting; DECLARE @Ident int; INSERT INTO Orders (CustomerNo,OrderDate, EmployeeID) VALUES (gETDATE,1); SELECT

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;

Page 17: SQL Server. اسکریپت درج مقدار در جدول USE Accounting; DECLARE @Ident int; INSERT INTO Orders (CustomerNo,OrderDate, EmployeeID) VALUES (gETDATE,1); SELECT

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;

Page 18: SQL Server. اسکریپت درج مقدار در جدول USE Accounting; DECLARE @Ident int; INSERT INTO Orders (CustomerNo,OrderDate, EmployeeID) VALUES (gETDATE,1); SELECT

مهار خطاها در اسکریپت

BEGIN TRY { <sql statement(s)> } END TRY BEGIN CATCH { <sql statement(s)> } END CATCH [ ; ]

Page 19: SQL Server. اسکریپت درج مقدار در جدول USE Accounting; DECLARE @Ident int; INSERT INTO Orders (CustomerNo,OrderDate, EmployeeID) VALUES (gETDATE,1); SELECT

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