Upload
jegadish1
View
212
Download
0
Embed Size (px)
Citation preview
8/18/2019 8.Insert,Upate
1/4
use AdventureWorks2008R2
INSERT [ INTO]table_or_view_name [ ( column_list ) ] VALUES (({DEFAULT | NULL | expression } [ ,...n ]) [ ,...n ])
---The comma-separated list of values to be inserted as a row into the table. --You can insert multiple rows in a single statement. -- Each value can be an expression, NULL value, or DEFAULT value
8-1. Inserting a New Row
INSERT INTO Production.Location (Name, CostRate, Availability) VALUES ('Wheel Storage', 11.25, 80.00) ;
8-2. Specifying Default Values
select * from Production.Location
insert into Production.Location (Name,CostRate,Availability) values ('jeish dba entry',22.00,14/8/2015)
INSERT Production.Location(Name,
CostRate,Availability,ModifiedDate)VALUES ('Wheel Storage 3',11.25,80.00,DEFAULT)
-- if a colmn is set any default value,we can do mention the same while inserting any row
select * fINSERT dbo.ExampleTable DEFAULT VALUES ;
---------------------
8-3. Overriding an IDENTITY Column--When set ON, explicit value inserts are allowed. When set OFF, explicit valueinserts are not allowed.
SET IDENTITY_INSERT [database_name.[schema_name].]table { ON | OFF }
------------8-4. Generating a Globally Unique Identifier (GUID)
select * from Purchasing.ShipMethod--NEWID system function generates a new GUID that can be inserted into a columndefined with UNIQUEIDENTIFIER.
INSERT Purchasing.ShipMethod(Name,ShipBase,ShipRate,rowguid)VALUES ('MIDDLETON CARGO TS1',
8/18/2019 8.Insert,Upate
2/4
8.99,1.22,NEWID()) ;
-------------8-5. Inserting Results from a Query
SELECT ShiftID,Name,StartTime,EndTime,ModifiedDateFROM HumanResources.ShiftORDER BY ShiftID ;
-----------
8-6. Inserting Results from a Stored Procedure
A stored procedure groups one or more Transact-SQL statements into a logical unit and stores it as an object
in a SQL Server database.
Stored procedures allow for more sophisticated result set creation (for example, youcan use several intermediate result sets built in temporary tables before returning the final result set).
Stored procedures that return a result set can be used with the INSERT...EXEC form of the INSERT statement.
INSERT [INTO] table_or_view_name
[(column_l ist)] EXEC stored_procedure_name
CREATE PROCEDURE dbo.usp_SEL_Production_TransactionHistory@ModifiedStartDT DATETIME,@ModifiedEndDT DATETIMEASSELECT TransactionID,ProductID,ReferenceOrderID,ReferenceOrderLineID,TransactionDate,TransactionType,Quantity,ActualCost,
ModifiedDateFROM Production.TransactionHistoryWHERE ModifiedDate BETWEEN @ModifiedStartDTAND @ModifiedEndDTAND TransactionID NOT IN (SELECT TransactionIDFROM Production.TransactionHistoryArchive) ;GO
use AdventureWorks2008R2
8/18/2019 8.Insert,Upate
3/4
CREATE PROCEDURE dbo.usp_SEL_Production_TransactionHistory@ModifiedStartDT DATETIME,@ModifiedEndDT DATETIMEASSELECT TransactionID,ProductID,ReferenceOrderID,ReferenceOrderLineID,TransactionDate,TransactionType,Quantity,ActualCost,ModifiedDateFROM Production.TransactionHistoryWHERE ModifiedDate BETWEEN @ModifiedStartDT AND @ModifiedEndDTAND TransactionID NOT IN ( SELECT TransactionID FROM Production.TransactionHistoryArchive) ;GO
EXEC dbo.usp_SEL_Production_TransactionHistory '2007-09-01', '2007-09-02'
-----------
select * from Production.TransactionHistoryArchive-------------- inserting the records with the store procedure,using insert and exec function
INSERT Production.TransactionHistoryArchive(TransactionID,ProductID,ReferenceOrderID,ReferenceOrderLineID,TransactionDate,TransactionType,Quantity,ActualCost,
ModifiedDate)EXEC dbo.usp_SEL_Production_TransactionHistory '2007-09-01','2007-09-02' ;
---------------------SELECT TransactionID,ProductID,ReferenceOrderID,ReferenceOrderLineID,TransactionDate,TransactionType,Quantity,ActualCost,ModifiedDate
FROM Production.TransactionHistoryWHERE ModifiedDate BETWEEN '2007-09-01' AND '2007-09-02' -- returns 4171 rows
8-7. Inserting Multiple Rows at Once
CREATE TABLE HumanResources.Degree(DegreeID INT NOT NULL IDENTITY(1, 1) PRIMARY KEY,DegreeName VARCHAR(30) NOT NULL,
8/18/2019 8.Insert,Upate
4/4
DegreeCode VARCHAR(5) NOT NULL,ModifiedDate DATETIME NOT NULL) ;GO
select * from HumanResources.Degree
--Next, insert multiple rows into the new table:
INSERT INTO HumanResources.Degree (DegreeName, DegreeCode, ModifiedDate)VALUES ('Bachelor of Arts', 'B.A.', GETDATE()),('Bachelor of Science', 'B.S.', GETDATE()),('Master of Arts', 'M.A.', GETDATE()),('Master of Science', 'M.S.', GETDATE()),('Associate" s Degree', 'A.A.', GETDATE()) ;GO
--------------
8-8. Inserting Rows and Returning the Inserted Rows
----------
8-9. Updating a Single Row or Set of Rows
SELECT DiscountPctFROM Sales.SpecialOfferWHERE SpecialOfferID = 10 ;
select * from Sales.SpecialOffer
order by SpecialOfferID
UPDATE Sales.SpecialOfferSET DiscountPct = 0.15WHERE SpecialOfferID = 10 ;-------------