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

Slides:



Advertisements
Similar presentations
Copyright © 2003 Pearson Education, Inc. Slide 8-1 The Web Wizards Guide to PHP by David Lash.
Advertisements

آموزش SQL Server 2005.
Batches, Scripts, Transactions-SQL Server 7. A batch is a set of Transact-SQL statements that are interpreted together by SQL Server. They are submitted.
Transact-SQL. 1. Declare float = 10 select * from customers where discnt
Copyright 2007, Information Builders. Slide 1 Walter Brengel Information Builders New Jersey User Forum – June 2008 FOCUS to SQL DeMystified.
Stored procedures Procedural programming in Microsoft SQL Server 1Stored procedures.
2015 International TechNet Wiki Summit 2015 Saeid Hasani Structured Error Handling Mechanism in SQL Server 2012 & 2014.
1 Chapter 8 – Working with Databases spring into PHP 5 by Steven Holzner Slides were developed by Jack Davis College of Information Science and Technology.
Slide 8-1 CHAPTER 8 Using Databases with PHP Scripts: Using MySQL Database with PHP.
INTERNET APPLICATION DEVELOPMENT For More visit:
A Comedy of Errors Handling Errors in T-SQL Code Andrew Whettam.
Module 12 Handling Errors in T-SQL Code. Module Overview Understanding T-SQL Error Handling Implementing T-SQL Error Handling Implementing Structured.
IMS 4212: Application Architecture and Intro to Stored Procedures 1 Dr. Lawrence West, Management Dept., University of Central Florida
Copyright © 2003 Pearson Education, Inc. Slide 8-1 The Web Wizard’s Guide to PHP by David Lash.
Introduction to Databases Chapter 7: Data Access and Manipulation.
Triggers Event handlers in the DBMS. Triggers are event handlers Triggers a executed when an event happens in the DBMS Example events – INSERT, UPDATE.
Dinamic SQL & Cursor. Why Dinamic SQL ? Sometimes there is a need to dynamically create a SQL statement on the fly and then run that command. This can.
Stored Procedures, Transactions, and Error-Handling
Module 8: Implementing Stored Procedures. Introducing Stored Procedures Creating, Modifying, Dropping, and Executing Stored Procedures Using Parameters.
Module 1: Introduction to Transact-SQL
Module 9 Designing and Implementing Stored Procedures.
SQL Server 7.0 Maintaining Referential Integrity.
SQL pepper. Why SQL File I/O is a great deal of code Optimal file organization and indexing is critical and a great deal of code and theory implementation.
T-SQL Transact-SQL is microsoft implementation of SQL. It contains additional programming constracts T-SQL enables you to write programs that contain SQL.
1 Definition of a subquery Nested subqueries Correlated subqueries The ISNULL function Derived tables The EXISTS operator Mixing data types: CAST & CONVERT.
SQL pepper. Why SQL File I/O is a great deal of code Optimal file organization and indexing is critical and a great deal of code and theory implementation.
SQL Data Definition Language (DDL) Using Microsoft SQL Server 1SDL Data Definition Language (DDL)
SQL SERVER.  DML Triggers  DDL Trigers  INSERT triggers  DELETE triggers  UPDATE triggers  A mix and match of any of the above.
Deanne Larson.  Variables allow you to store data values temporarily for later use in the same batch where they were declared. I describe batches later.
Copyright © Curt Hill Stored Procedures In Transact-SQL.
SELECT e.NationalIDNumber, p.FirstName,p.LastName, City FROM HumanResources.Employee e INNER JOIN Person.Person p on p.BusinessEntityID = e.BusinessEntityID.
Stored Procedures Week 9. Test Details Stored Procedures SQL can call code written in iSeries High Level Languages –Called stored procedures SQL has.
How transactions work A transaction groups a set of Transact-SQL statements so that they are treated as a unit. Either all statements in the group are.
Module 8: Implementing Stored Procedures. Overview Implementing Stored Procedures Creating Parameterized Stored Procedures Working With Execution Plans.
Constraints cis 407 Types of Constraints & Naming Key Constraints Unique Constraints Check Constraints Default Constraints Misc Rules and Defaults Triggers.
06 | Modifying Data in SQL Server Brian Alderman | MCT, CEO / Founder of MicroTechPoint Tobias Ternstrom | Microsoft SQL Server Program Manager.
IMS 4212: Data Manipulation 1 Dr. Lawrence West, MIS Dept., University of Central Florida Additional Data Manipulation Statements INSERT.
How Microsoft Great Plains eEnterprise Utilizes SQL Server William Boynes Jr.
SQL Server User Defined Functions. CREATE FUNCTION [ schema_name. ] function_name ( [ [ AS ][ type_schema_name. ] parameter_data_type.
Database projects in visual studio 2010 Anthony Brown
Best Practices in SQL Programming. Do not use irrelevant datatype  VARCHAR instead of DATETIME  CHAR(N) instead of VARCHAR(N)  etc.
G. Green 1.  Options include:  Script Files  already covered  APIs  last course topic  Database-Stored Code  our focus 2.
IBuySPY Shopping Store. Data Model for IBuySPY Shopping Store.
Learningcomputer.com SQL Server 2008 –Views, Functions and Stored Procedures.
Stored Procedure Optimization Preventing SP Time Out Delay Deadlocking More DiskReads By: Nix.
Ch 5. Introducing More Database Objects. Database Objects Table (ch2) View (ch3) Stored Procedure Trigger Function User-defined types.
IMS 4212: Application Architecture and Intro to Stored Procedures 1 Dr. Lawrence West, Management Dept., University of Central Florida
IMS 4212: Constraints & Triggers 1 Dr. Lawrence West, Management Dept., University of Central Florida Stored Procedures in SQL Server.
Ch 7. Working with relational data. Transactions Group of statements executed as a group. If all statements execute successfully, changes are committed.
Module 9: Using Advanced Techniques. Considerations for Querying Data Working with Data Types Cursors and Set-Based Queries Dynamic SQL Maintaining Query.
Module 8: Using Programming Objects for Data Retrieval.
Dynamic SQL Writing Efficient Queries on the Fly ED POLLACK AUTOTASK CORPORATION DATABASE OPTIMIZATION ENGINEER.
In this session, you will learn to: Create and manage views Implement a full-text search Implement batches Objectives.
Module 9: Implementing Functions. Overview Creating and Using Functions Working with Functions Controlling Execution Context.
Implementing Functions Advanced Database Dr. AlaaEddin Almabhouh.
T-SQL Scripts Advanced Database Dr. AlaaEddin Almabhouh.
1 c6212 Advanced Database and Client Server MS SQL Server 2000 Stored Procedures and Parameters What ? Why ? How ?
Advanced SQL - DDL Advanced Database Dr. AlaaEddin Almabhouh.
BE PREPARED Error Handling & Optimizing T-SQL Code in SQL Server 2000
Task oriented processing
11 | Error Handling and Transactions
PROCEDURES, CONDITIONAL LOGIC, EXCEPTION HANDLING, TRIGGERS
Objectives Describe an overview of Transact-SQL programming
Dynamic SQL Writing Efficient Queries on the Fly
T-SQL Transact - Structured Query Language (Microsoft)
Module 5: Implementing Data Integrity by Using Constraints
SQL Server Stored Procedures.
Exploring without light….
CS4540 Special Topics in Web Development SQL and MS SQL
Presentation transcript:

SQL Server

اسکریپت درج مقدار در جدول USE Accounting; int; INSERT INTO Orders (CustomerNo,OrderDate, EmployeeID) VALUES (gETDATE,1); = INSERT INTO OrderDetails (OrderID, PartNo, Description, UnitPrice, Qty) VALUES ‘2R2416’, ‘Cylinder Head’, 1300, 2); SELECT ‘The OrderID of the INSERTed row is ‘ +

SET استفاده از  USE AdventureWorks2008;  money;  = (SELECT MAX)UnitPrice) FROM Sales.SalesOrderDetail) 

برای انتخاب SELECT استفاده از  USE AdventureWorks2008;  money;  = MAX)UnitPrice) FROM Sales.SalesOrderDetail; 

SQL Server چند متغیر سیستمی       

 USE AdventureWorks2008;  GO  int;  SELECT * FROM Person.Person;   =

Batch USE AdventureWorks2008; varchar)50); --This DECLARE only lasts for this batch! = ‘Honey, I’’m home...’; PRINT ‘Done with first Batch...’; GO --This generates an error --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  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 ] [ -o ]   SQLCMD -Usa -Pmypass –Q “SELECT * FROM AdventureWorks2008.Production.Locati on”

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  { }  END TRY  BEGIN CATCH  { }  END CATCH [ ; ]

 BEGIN TRY  CREATE TABLE OurIFTest(Col1 int PRIMARY KEY);  END TRY  BEGIN CATCH  int, smallint, nvarchar(4000);  = ERROR_NUMBER(), = ERROR_SEVERITY(), = = ERROR_LINE (), = ERROR_MESSAGE();  = 2714  PRINT ‘WARNING: Skipping CREATE as table already exists’;  ELSE 16, 1 );  END CATCH