SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


DML in Functions


DML in Functions

Author
Message
mithun.gite
mithun.gite
SSC-Addicted
SSC-Addicted (441 reputation)SSC-Addicted (441 reputation)SSC-Addicted (441 reputation)SSC-Addicted (441 reputation)SSC-Addicted (441 reputation)SSC-Addicted (441 reputation)SSC-Addicted (441 reputation)SSC-Addicted (441 reputation)

Group: General Forum Members
Points: 441 Visits: 364
Dear friends,

Need understanding, that y v cant perform DMLs in Functions?
We can use DMLs using table variable in fucntion then y can with actual tables..

Please guide me thru..

Mithun
ChrisM@Work
ChrisM@Work
SSC-Insane
SSC-Insane (22K reputation)SSC-Insane (22K reputation)SSC-Insane (22K reputation)SSC-Insane (22K reputation)SSC-Insane (22K reputation)SSC-Insane (22K reputation)SSC-Insane (22K reputation)SSC-Insane (22K reputation)

Group: General Forum Members
Points: 22173 Visits: 19709
mithun.gite (4/22/2009)
Dear friends,

Need understanding, that y v can perform DMLs in Functions?
We can use DMLs using table variable in fucntion then y can with actual tables..

Please guide me thru..

Mithun


From BOL:
The following statements are valid in a function:

Assignment statements.


Control-of-Flow statements except TRY...CATCH statements.


DECLARE statements defining local data variables and local cursors.


SELECT statements that contain select lists with expressions that assign values to local variables.


Cursor operations referencing local cursors that are declared, opened, closed, and deallocated in the function. Only FETCH statements that assign values to local variables using the INTO clause are allowed; FETCH statements that return data to the client are not allowed.


INSERT, UPDATE, and DELETE statements modifying local table variables.


EXECUTE statements calling extended stored procedures.


For more information, see Creating User-defined Functions (Database Engine).


Can you explain what you are trying to do?

“Write the query the simplest way. If through testing it becomes clear that the performance is inadequate, consider alternative query forms.” - Gail Shaw

For fast, accurate and documented assistance in answering your questions, please read this article.
Understanding and using APPLY, (I) and (II) Paul White
Hidden RBAR: Triangular Joins / The "Numbers" or "Tally" Table: What it is and how it replaces a loop Jeff Moden
Exploring Recursive CTEs by Example Dwain Camps
mithun.gite
mithun.gite
SSC-Addicted
SSC-Addicted (441 reputation)SSC-Addicted (441 reputation)SSC-Addicted (441 reputation)SSC-Addicted (441 reputation)SSC-Addicted (441 reputation)SSC-Addicted (441 reputation)SSC-Addicted (441 reputation)SSC-Addicted (441 reputation)

Group: General Forum Members
Points: 441 Visits: 364
Chris Morris (4/22/2009)
mithun.gite (4/22/2009)
Dear friends,

Need understanding, that y v can perform DMLs in Functions?
We can use DMLs using table variable in fucntion then y can with actual tables..

Please guide me thru..

Mithun


From BOL:
The following statements are valid in a function:

Assignment statements.


Control-of-Flow statements except TRY...CATCH statements.


DECLARE statements defining local data variables and local cursors.


SELECT statements that contain select lists with expressions that assign values to local variables.


Cursor operations referencing local cursors that are declared, opened, closed, and deallocated in the function. Only FETCH statements that assign values to local variables using the INTO clause are allowed; FETCH statements that return data to the client are not allowed.


INSERT, UPDATE, and DELETE statements modifying local table variables.


EXECUTE statements calling extended stored procedures.


For more information, see Creating User-defined Functions (Database Engine).


Can you explain what you are trying to do?


thanks chris for ur reply...........
after seeing ur reply i chekced my question which was bit wrong, which i corrected, actually i wan to know y we cant use DMLs in Functions?

Sorry for the trouble.......
And i m nothing trying but its been asked to me and i was clueless so i put it here..

Mithun
ChrisM@Work
ChrisM@Work
SSC-Insane
SSC-Insane (22K reputation)SSC-Insane (22K reputation)SSC-Insane (22K reputation)SSC-Insane (22K reputation)SSC-Insane (22K reputation)SSC-Insane (22K reputation)SSC-Insane (22K reputation)SSC-Insane (22K reputation)

Group: General Forum Members
Points: 22173 Visits: 19709
From BOL, the first few sentences for CREATE FUNCTION:
Creates a user-defined function. This is a saved Transact-SQL or common language runtime (CLR) routine that returns a value. User-defined functions cannot be used to perform actions that modify the database state.


The purpose of a function is to return a value. For a simplistic model of why DML isn't supported in functions, consider this: you run a query which includes a function call against a table
SELECT *, myResult = dbo.fnDML(GETDATE(), dbo.fnGetWeather(GETDATE(), 'Reading, UK')) ...
- what's it going to do, add a column to a table if today is wednesday and it's sunny? Worse still, you could drop or add columns or even tables according to values in your source table. Row by row, the schema could change.
The structure of your db, which took months of careful design, is now all over the place.

“Write the query the simplest way. If through testing it becomes clear that the performance is inadequate, consider alternative query forms.” - Gail Shaw

For fast, accurate and documented assistance in answering your questions, please read this article.
Understanding and using APPLY, (I) and (II) Paul White
Hidden RBAR: Triangular Joins / The "Numbers" or "Tally" Table: What it is and how it replaces a loop Jeff Moden
Exploring Recursive CTEs by Example Dwain Camps
mithun.gite
mithun.gite
SSC-Addicted
SSC-Addicted (441 reputation)SSC-Addicted (441 reputation)SSC-Addicted (441 reputation)SSC-Addicted (441 reputation)SSC-Addicted (441 reputation)SSC-Addicted (441 reputation)SSC-Addicted (441 reputation)SSC-Addicted (441 reputation)

Group: General Forum Members
Points: 441 Visits: 364
Hey chris sir,,,

thanks for ur reply... now i know y we cant use dml in functions...
thanks , really thats usefulll

Mithun
ChrisM@Work
ChrisM@Work
SSC-Insane
SSC-Insane (22K reputation)SSC-Insane (22K reputation)SSC-Insane (22K reputation)SSC-Insane (22K reputation)SSC-Insane (22K reputation)SSC-Insane (22K reputation)SSC-Insane (22K reputation)SSC-Insane (22K reputation)

Group: General Forum Members
Points: 22173 Visits: 19709
You're welcome - and I'm no 'sir', just a common working person, like yourself :-D

“Write the query the simplest way. If through testing it becomes clear that the performance is inadequate, consider alternative query forms.” - Gail Shaw

For fast, accurate and documented assistance in answering your questions, please read this article.
Understanding and using APPLY, (I) and (II) Paul White
Hidden RBAR: Triangular Joins / The "Numbers" or "Tally" Table: What it is and how it replaces a loop Jeff Moden
Exploring Recursive CTEs by Example Dwain Camps
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search