0% found this document useful (0 votes)
12 views2 pages

Dynamic SQL

The document contains SQL commands to create a Sales table, insert sales data for the years 2024 and 2025, and perform aggregate queries to summarize sales by month and year. It also includes dynamic SQL execution examples and stored procedures for retrieving sales data based on input parameters. Additionally, there is a procedure for querying employee data with optional filters for first name and last name.

Uploaded by

sdivya3103
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views2 pages

Dynamic SQL

The document contains SQL commands to create a Sales table, insert sales data for the years 2024 and 2025, and perform aggregate queries to summarize sales by month and year. It also includes dynamic SQL execution examples and stored procedures for retrieving sales data based on input parameters. Additionally, there is a procedure for querying employee data with optional filters for first name and last name.

Uploaded by

sdivya3103
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 2

CREATE TABLE dbo.

Sales (
salesId INT PRIMARY KEY IDENTITY(1,1),
[Year] INT,
[Month] VARCHAR(200),
Amount DECIMAL(10, 2)
);
INSERT INTO dbo.Sales (year, month, Amount) VALUES
(2025, 'January', 1000.00),
(2025, 'February', 1500.00),
(2025, 'March', 1200.00),
(2025, 'April', 1300.00),
(2025, 'May', 1100.00),
(2025, 'June', 1400.00),
(2025, 'July', 1600.00),
(2025, 'August', 1700.00),
(2025, 'September', 1800.00),
(2025, 'October', 1900.00),
(2025, 'November', 2000.00),
(2025, 'December', 2100.00),
(2024, 'January', 1000.00),
(2024, 'February', 1500.00),
(2024, 'March', 1200.00),
(2024, 'April', 1300.00),
(2024, 'May', 1100.00),
(2024, 'June', 1400.00),
(2024, 'July', 1600.00),
(2024, 'August', 1700.00);
SELECT * FROM dbo.Sales

SELECT [Month],SUM(Amount) MonthSales FROM dbo.Sales


GROUP BY [Month]

SELECT [Year],SUM(Amount) YearSales FROM dbo.Sales


GROUP BY [Year]

/*Basic Dynamic*/

DECLARE @sql NVARCHAR(200)


DECLARE @parms NVARCHAR(200)
SET @sql='select * from dbo.Sales' +' where year=@Year'
SET @Parms='@Year int'

EXEC sp_executesql @sql,@parms,@Year=2024

/*Create procedure*/

DECLARE @Input nVARCHAR(10)='year'


DECLARE @Function nVARCHAR(20)
DECLARE @DySQL nVARCHAR(1000)

IF(@Input='Month')
BEGIN
SET @Function='Month'
END
IF(@Input='Year')
BEGIN
SET @Function='Year'
END
SET @DySQL='SELECT ' + @Function + ',SUM(Amount) Total,avg(amount) avgamount
FROM dbo.Sales
GROUP BY '+ @Function

EXEC (@DySQL)

ALTER PROC #Sp_getamount


(@Input nVARCHAR(10)
)
AS
DECLARE @Function nVARCHAR(20)
DECLARE @DySQL nVARCHAR(1000)
BEGIN
IF(@Input='Month')
BEGIN
SET @Function='Month'
END
IF(@Input='Year')
BEGIN
SET @Function='Year'
END

SET @DySQL='SELECT ' + @Function + ',SUM(Amount) Total,avg(amount) avgamount


FROM dbo.Sales
GROUP BY '+ @Function
EXEC (@DySQL)
END
EXEC #Sp_getamount @Input='YEAR'

/*****/

ALTER PROCEDURE #SP_GoodDynamic


(@Firstname NVARCHAR(200)=NULL,
@Lastname NVARCHAR(200)=NULL,
@Gender NVARCHAR(200)=NULL,
@Salary INT=NULL)
AS
BEGIN

DECLARE @SQL NVARCHAR(2000)


SET @SQL='SELECT * FROM dbo.EmployeeData where 1=1'
IF (@Firstname IS NOT NULL)
SET @SQL=@SQL +'and Firstname =@FN'
IF (@Lastname IS NOT NULL)
SET @SQL=@SQL +'and Lastname =@LN'

EXEC sys.sp_executesql @sql,N'@FN NVARCHAR(200),@LN NVARCHAR(200)',


@FN=@Firstname,@LN=@Lastname
END
--EXEC #SP_GoodDynamic @Firstname='John'

You might also like