SQL Server Forums
Profile | Register | Active Topics | Members | Search | Forum FAQ
 
Register Now and get your question answered!
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 SQL Server 2008 Forums
 Transact-SQL (2008)
 Set based or Procedural based
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

Vishal_sql
Yak Posting Veteran

88 Posts

Posted - 07/09/2012 :  06:05:53  Show Profile  Reply with Quote
Hi Friends,
I am working as SQL Server Programmer ( Intermediate Level ) and writes stored procedures in my most of work.
I want to know Is it necessary to write the SQL Server Code in Set-based instead of procedural-based?

I know It needs practice but how can one adhere to Set based Coding methodolgy?

Transact Charlie
Flowing Fount of Yak Knowledge

United Kingdom
3425 Posts

Posted - 07/09/2012 :  06:26:40  Show Profile  Visit Transact Charlie's Homepage  Reply with Quote
One of the best things to do when coding a stored proc or similar is to ask:

How would I do this thing if I was given a List of values rather than one value?

And think in terms of columns rather than rows.

Transact Charlie

Msg 3903.. The ROLLBACK TRANSACTION request has no corresponding BEGIN TRANSACTION.
Go to Top of Page

Vishal_sql
Yak Posting Veteran

88 Posts

Posted - 07/09/2012 :  07:27:46  Show Profile  Reply with Quote
Hi Charles
Your comments are interesting.
Any example would have been good :)
i'll try.
Thanks
Go to Top of Page

sunitabeck
Flowing Fount of Yak Knowledge

5152 Posts

Posted - 07/09/2012 :  07:58:32  Show Profile  Reply with Quote
An example: Assume you have a table that lists all the products of your company. For example, this would create a sample table with 3 products.
CREATE TABLE #Products
(ProductId INT, UnitPrice DECIMAL(18,4));

INSERT INTO #Products VALUES
(1,201.75),
(2,33.4),
(3,98.77);
Now you are asked to raise the price of all products by 5%. How would you do it in T-SQL?
Go to Top of Page

Vishal_sql
Yak Posting Veteran

88 Posts

Posted - 07/09/2012 :  08:25:38  Show Profile  Reply with Quote
Hi Sunita,
In the first sight of looking at the problem it seems it isn't possible to solve it in single update statement and like most of programmers ( am not saying Developers ) I would go for Cursor.

Plz give ur suggestion on it

Thanks,
vishal_sql
Go to Top of Page

Vishal_sql
Yak Posting Veteran

88 Posts

Posted - 07/09/2012 :  08:35:58  Show Profile  Reply with Quote
I beleive we need to raise price of few Productid in table
and for that needs to use cursor (take the required productid and raise the price of those products in cursor)
Coming back to root of question How can we do it using SET based ?
Go to Top of Page

Transact Charlie
Flowing Fount of Yak Knowledge

United Kingdom
3425 Posts

Posted - 07/09/2012 :  09:07:58  Show Profile  Visit Transact Charlie's Homepage  Reply with Quote
the code to do it would be simply this:

UPDATE p SET
    [UnitPrice] = [UnitPrice] * 1.05
FROM
    products AS p


Transact Charlie

Msg 3903.. The ROLLBACK TRANSACTION request has no corresponding BEGIN TRANSACTION.
Go to Top of Page

sunitabeck
Flowing Fount of Yak Knowledge

5152 Posts

Posted - 07/09/2012 :  09:10:31  Show Profile  Reply with Quote
In set-based programming, instead of using a cursor or while loop, you would simply run a single update statement which would update all the rows.
UPDATE #Products SET UnitPrice = UnitPrice*1.05;
If you wanted to update only a subset then you could still use the set-based update but this time specifying a WHERE clause to do the updates only on the subset. If you were asked to update the prices only for products whose product id is less than or equal to two:
UPDATE #Products SET UnitPrice = UnitPrice*1.05
WHERE ProductId <= 2;
You can find lot of resources on set-based programming on the web. Also, many basic T-SQL books should cover the elements of it.
Go to Top of Page

Transact Charlie
Flowing Fount of Yak Knowledge

United Kingdom
3425 Posts

Posted - 07/09/2012 :  09:13:59  Show Profile  Visit Transact Charlie's Homepage  Reply with Quote
and if you wanted to do this for only some group of productId's then you could do something like this:

DECLARE @products TABLE (
	[ProductID] INT
	, [UnitCost] MONEY
	)

INSERT @products ([ProductID], [UnitCost]) VALUES
(1,201.75),
(2,33.4),
(3,98.77);


-- Display Products
SELECT * FROM @products

-- Update all prices by 5%
UPDATE p SET [UnitCost] = [UnitCost] * 1.05 FROM @products AS p

SELECT 'After all upped by 5%', * FROM @products

-- Update some list of prices by 5% (Products 1 and 3)
DECLARE @updates TABLE ( [ProductID] INT )
INSERT @updates([ProductID]) VALUES (1), (3)

UPDATE p SET [UnitCost] = [UnitCost] * 1.05
FROM
	@products AS p
	JOIN @updates AS u ON u.[ProductID] = p.[ProductID]
	
SELECT 'After 1 and 3 upped by 5%', * FROM @products


Transact Charlie

Msg 3903.. The ROLLBACK TRANSACTION request has no corresponding BEGIN TRANSACTION.
Go to Top of Page

Vishal_sql
Yak Posting Veteran

88 Posts

Posted - 07/09/2012 :  09:56:07  Show Profile  Reply with Quote
It means for solving any problem you have to think that I will solve the problem without using Cursor or while loop and try to do it in single query.( except some cases where should use cursor's eg:- SSRS queries)
Great comments Sunita and Charles.Thanks.
It would definately help me.

Go to Top of Page

Transact Charlie
Flowing Fount of Yak Knowledge

United Kingdom
3425 Posts

Posted - 07/09/2012 :  10:00:52  Show Profile  Visit Transact Charlie's Homepage  Reply with Quote
if you just remember that a single value is just a special case of multiple values then you'll do fine.



Transact Charlie

Msg 3903.. The ROLLBACK TRANSACTION request has no corresponding BEGIN TRANSACTION.
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

India
47034 Posts

Posted - 07/09/2012 :  10:05:19  Show Profile  Reply with Quote
quote:
Originally posted by Vishal_sql

It means for solving any problem you have to think that I will solve the problem without using Cursor or while loop and try to do it in single query.( except some cases where should use cursor's eg:- SSRS queries)
Great comments Sunita and Charles.Thanks.
It would definately help me.




Not exactly. Why should you use cursors for SSRS queries? it can also be using set based techniques and really depends on scenario. You cant predetermine that it requires cursor.

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

Vishal_sql
Yak Posting Veteran

88 Posts

Posted - 07/09/2012 :  10:19:42  Show Profile  Reply with Quote
Hi Visakh ,
I Agreed on your comments.One will use the Cursor depending on the scenarios.
And m happy u jumped in between.
We were discussing When can we use Cusror and when can we use Set based approach.
I heard that reporting queries requires cursor as most of calculations cant be done in single queries.
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

India
47034 Posts

Posted - 07/09/2012 :  11:00:30  Show Profile  Reply with Quote
quote:
Originally posted by Vishal_sql

Hi Visakh ,
I Agreed on your comments.One will use the Cursor depending on the scenarios.
And m happy u jumped in between.
We were discussing When can we use Cusror and when can we use Set based approach.
I heard that reporting queries requires cursor as most of calculations cant be done in single queries.


thats not mostly true. I've been developing SSRS reports for over 6 years and during this time i never had to use cursors for my queries. Most cases you can come up with set based alternative for your cursor related queries.

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page
  Previous Topic Topic Next Topic  
 New Topic  Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.09 seconds. Powered By: Snitz Forums 2000