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

Query in SQL Server to get the Parameter list of given Store Procedure

Write a Query in SQL Server to get the Parameter list of given Store Procedure.

Suppose we want to get the name of the parameter for the Store Procedure
Course_Insert_sp then folllowing needs to be executed.
SELECT * FROM sys.parameters D INNER JOIN Sys.Objects O ON O.object_id=D.object_id WHERE O.name='Course_Insert_sp'

Query to Get the Forgien Key Name Their Primary Table and Referrence Table

Following Query will give you the List of Forgien Key in a Database with Their Primary and Referrence Tables Name.

Sample Query:
SELECT F.name AS ForgienKey,O2.name AS ParentTable,O3.name AS RefTable FROM sys.foreign_keys F
INNER JOIN Sys.Objects O2 ON O2.object_id=F.parent_object_id
INNER JOIN Sys.Objects O3 ON O3.object_id=F.referenced_object_id
If you want to get forgien key for a particular table then you can use like this:
SELECT F.name AS ForgienKey,O2.name AS ParentTable,O3.name AS RefTable FROM sys.foreign_keys F
INNER JOIN Sys.Objects O2 ON O2.object_id=F.parent_object_id
INNER JOIN Sys.Objects O3 ON O3.object_id=F.referenced_object_id
Where O3.name='Tbl_User'

Query to get the name of the identity column for a given table

Query to get the name of the identity column for a given table.
To Get the Identity column name for Tbl_Customer we need to execute the following query.
SELECT D.name AS ColumnName, O.name AS TableName
FROM sys.identity_columns D INNER JOIN Sys.Objects O ON O.object_id=D.object_id
Where O.name='Tbl_Customer

Query to Get the dependencies for given Table Name

Following Query is used to get the dependencies for the given Table.
Suppose we need to check the dependencies for the table tbl_Customer

Sample Query:
DECLARE @Objectname varchar(100)
SET @Objectname='tbl_Customer'

SELECT O1.name,O2.name FROM sys.sql_dependencies D INNER JOIN Sys.Objects O1 ON O1.object_id=D.object_id
INNER JOIN Sys.Objects O2 ON O2.object_id=D.referenced_major_id
WHERE O1.name=@Objectname OR O2.name=@Objectname

Get Total Number of a Particular Day between Two Dates

From this Set of Query we can find out the occurance of a particular day between two given dates.

if you want to get calculate total no of sat then set @DayNumber=7 and for Sun Set @DayNumber=1, Mon @DayNumber=2 and so on..

DECLARE @Count int, @Startdate Datetime,@EndDate datetime, @DayNumber int
SET @Count=0
SET @Startdate=GETDATE()-1000
SET @EndDate=GETDATE()
SET @DayNumber=7
DECLARE @StartDay Int

WHILE(@EndDate>@Startdate)
BEGIN
SET @StartDay=DATEPART(dw, @Startdate)
if(@StartDay=7)
BEGIN
SET @Count=@Count+1
SET @Startdate=@Startdate+7
END
else
BEGIN
SET @StartDay=@DayNumber-@StartDay
SET @Startdate=@Startdate+@StartDay
END
END
SELECT @Count