﻿<?xml version='1.0' encoding='UTF-8'?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/"><channel><title>SQLServerCentral / SQL Server 2005 / SQL Server 2005 General Discussion  / DML in Functions / Latest Posts</title><generator>InstantForum.NET v2.9.0</generator><description>SQLServerCentral</description><link>http://www.sqlservercentral.com/Forums/</link><webMaster>notifications@sqlservercentral.com</webMaster><lastBuildDate>Sun, 19 May 2013 15:21:33 GMT</lastBuildDate><ttl>20</ttl><item><title>RE: DML in Functions</title><link>http://www.sqlservercentral.com/Forums/Topic702100-149-1.aspx</link><description>You're welcome - and I'm no 'sir', just a common working person, like yourself :-D</description><pubDate>Wed, 22 Apr 2009 02:55:42 GMT</pubDate><dc:creator>ChrisM@Work</dc:creator></item><item><title>RE: DML in Functions</title><link>http://www.sqlservercentral.com/Forums/Topic702100-149-1.aspx</link><description>Hey chris sir,,,thanks for ur reply... now i know  y we cant use dml in functions...thanks , really thats usefulllMithun</description><pubDate>Wed, 22 Apr 2009 02:48:36 GMT</pubDate><dc:creator>mithun.gite</dc:creator></item><item><title>RE: DML in Functions</title><link>http://www.sqlservercentral.com/Forums/Topic702100-149-1.aspx</link><description>From BOL, the first few sentences for CREATE FUNCTION:[quote]Creates a user-defined function. This is a saved Transact-SQL or common language runtime (CLR) routine [b]that returns a value[/b]. User-defined functions cannot be used to perform actions that modify the database state. [/quote]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 tableSELECT *, 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 [i]values[/i] in your source table. Row by row, the schema could change.The structure of your db, which took [i]months[/i] of careful design, is now all over the place.</description><pubDate>Wed, 22 Apr 2009 02:26:02 GMT</pubDate><dc:creator>ChrisM@Work</dc:creator></item><item><title>RE: DML in Functions</title><link>http://www.sqlservercentral.com/Forums/Topic702100-149-1.aspx</link><description>[quote][b]Chris Morris (4/22/2009)[/b][hr][quote][b]mithun.gite (4/22/2009)[/b][hr]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[/quote]From BOL:[quote]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).[/quote]Can you explain what you are trying to do?[/quote]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</description><pubDate>Wed, 22 Apr 2009 02:00:57 GMT</pubDate><dc:creator>mithun.gite</dc:creator></item><item><title>RE: DML in Functions</title><link>http://www.sqlservercentral.com/Forums/Topic702100-149-1.aspx</link><description>[quote][b]mithun.gite (4/22/2009)[/b][hr]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[/quote]From BOL:[quote]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).[/quote]Can you explain what you are trying to do?</description><pubDate>Wed, 22 Apr 2009 01:52:17 GMT</pubDate><dc:creator>ChrisM@Work</dc:creator></item><item><title>DML in Functions</title><link>http://www.sqlservercentral.com/Forums/Topic702100-149-1.aspx</link><description>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</description><pubDate>Wed, 22 Apr 2009 01:42:23 GMT</pubDate><dc:creator>mithun.gite</dc:creator></item></channel></rss>