Showing posts with label MS SQL. Show all posts
Showing posts with label MS SQL. Show all posts

Saturday, 10 March 2012

MS SQL 2012 - How to Create Simple Stored Procedure with Scope Identity



Scope_Identity() - returns last inserted record id (primary key) in scope (for instance stored procedure)

ALTER PROCEDURE [dbo].[InsertDept]
    @DeptName Varchar(50)
AS
BEGIN
    -- SET NOCOUNT ON added to prevent extra result sets from
    -- interfering with SELECT statements.
    SET NOCOUNT ON;

    Insert Into Dpt(DepartmentName) Values(@DeptName);
    Select Scope_Identity() as NewDepartment;
END

if you execute this stored procedure you will get id of the new department

Tuesday, 21 February 2012

MS SQL 2012 - How to Insert Multiple Records Into Table

Insert Into Customer(CustomerFirstName, CustomerLastName, CustomerAddress1)
Values
('Elvis', 'Costello', '22 Main Street'),
('Gordon', 'Summer', '33 Main Street'),
('Gordon', 'Summer', '33 Main Street')



MS SQL 2012 - How to Convert First letter of a string to upper case and remaining letters to lower case

Select UPPER(LEFT(RTRIM(LTRIM(CustomerFirstName)), 1)) +
LOWER(SUBSTRING(RTRIM(LTRIM(CustomerFirstName)), 2, LEN(CustomerFirstName) - 1))
 from Customer



MS SQL 2012 - How to Concatenate 2 Integers

SELECT CAST(CustomerId AS varchar) + '.' + CAST(CustomerAge AS Varchar) AS Expr1 FROM Customer

MS SQL 2012 - How to List all column names from particular table

SELECT * FROM INFORMATION_SCHEMA.Columns WHERE TABLE_NAME = 'YourTableName'

MS SQL 2012 - How to Kill running process

Right click server name in object explorer
Select Activity Monitor.
Kill selected process.

MS SQL 2012 - How to Convert datetime to date

CONVERT(DATE,GETDATE())

MS SQL 2012 - How to Create simple database

This video shows how to create simple database with log file that never exceeds 100Mb