Create proc spGetName
@pInfo varchar(50) output
as
Set @pInfo = (select Name from tblPersons where Name = 'Tom')
output ---->
Output parameters behave just like input parameters, however these output parameter needs to be declared with the OUTPUT keyword when used in a stored procedure.Set @pInfo = (select Name from tblPersons where Name = Tom)
----> Store the result of the query in an output variable.Declare @pMyID int = spGetID
Declare
----> the Declare keyword is used followed by the variable name and its data type when declaring a variable. Exec T @pMyID output
select @pMyID
The Rank function provides the rank of each row from a table.
1. To get the rank of rows based on their ID column, type:
select *,
rank() over (Order by ID) as 'Rank'
From tblPersons
rank() over (Order by ID) ----> The rank() keyword must be followed by the over keyword and an aggregate function to order to function.
In this case the order by aggregate function is used.
2. Execute the query.
Result:
select * from tblPersons
order by id
offset 0
rows fetch first 10 rows only
offset 0
----> Rows to be skipped = 0rows fetch first 10 rows only
----> Divide the records into first 10 per pagerows fetch next 10 rows only
if you want each next 10 records- lets say in page 2. select Name from tblPersons
where tblPersons.ID = (select top 1 id from tblPersons as ID)
Create table ##tblTemp
(ID int,
Name varchar(50));
#tblTemp
----> The '#' character is used to define a temporary table.##tblTemp
----> 2 '#' characters are used to define a global temporary table.select * from ##tblTemp
declare @varTbl table
(ID int,
Name varchar(50))
declare @varTbl table
----> the table keyword is used as the data type of the variable to make it behave like a tableselect * from @varTbl
truncate table tblCars
if 1 > 0
select '1 is greater than 0'
else if 1 > 2
select '1 is greater than 2'
else
select '1 is less'
if 1 > 0
----> the if keyword is followed by a value to be evaluted followed by a conditionelse if 1 > 2
----> executes only when the if statement evaluates to falseselect '1 is greater'
----> this statement will only execute if the if statement evaluates to true.else select '1 Is less'
----> Default statement to be executed in case the if and the else if statement evaluate to false.WHILE (SELECT AVG(salary) FROM tblEmployee) > 3000
BEGIN
SELECT 'The average employee salary is greater than $3000'
BREAK
END
WHILE (SELECT AVG(salary) FROM tblEmployee) > 3000
----> The while keyword is followed by a condition (while average salary is less than 3000)CREATE PROCEDURE sp_Example
AS
SELECT * FROM NonexistentTable;
GO
BEGIN TRY
EXECUTE sp_Example
END TRY
BEGIN CATCH
SELECT
ERROR_NUMBER() AS ErrorNumber
,ERROR_MESSAGE() AS ErrorMessage
END CATCH
BEGIN TRY
EXECUTE sp_Example END TRY
----> T-SQL statements are inside in the TRY block.BEGIN CATCH
SELECT
ERROR_NUMBER() AS ErrorNumber
, ERROR_MESSAGE() AS ErrorMessage END CATCH
----> In case an error is found in that TRY block, T-SQL statements that are inside the CATCH block are executed.Create table tblName
(
id int,
name varchar(50)
)
with(Data_Compression = Row)
Alter database SomethingDB
set Read_Only
Exec Master.dbo.sp_AddumpDevice
@Devtype = N'Disk'
,@LogicalName = N'SomethingDB_Backup_Dev'
,@physicalName =N'C:\SomethingDB.bak'
use SomethingDB
Go
use SomethingDB
----> We need to specify the name of the database to be used.@Devtype = N'Disk'
----> Devtype stands for device type. In this case we set it to N'Disk' since the backup is being stored on a hard disk drive. @LogicalName
----> The logical name is used by SQL server to reference the backup file. The logical name must be unique within your database.@physicalName =N'C:\SomethingDB.bak'
----> Note that the file path for the backup may vary depending on how you setup storage on your computer.Exec Master.dbo.sp_AddumpDevice
----> The master database name is specified since it contains the store procedure sp_AddumpDevice which we are using.