diff -u ejabberd_auth_odbc.erl ejabberd_auth_odbc.erl
--- ejabberd_auth_odbc.erl	2005-11-22 12:56:25.000000000 +0100
+++ ejabberd_auth_odbc.erl	2005-11-22 12:59:07.000000000 +0100
@@ -48,8 +48,7 @@
 	    Username = ejabberd_odbc:escape(LUser),
 	    case catch ejabberd_odbc:sql_query(
 			 jlib:nameprep(Server),
-			 ["select password from users "
-			  "where username='", Username, "'"]) of
+			 ["EXECUTE dbo.get_password ""'", Username, "'"]) of
 		{selected, ["password"], [{Password}]} ->
 		    true;
 		_ ->
@@ -65,8 +64,7 @@
 	    Username = ejabberd_odbc:escape(LUser),
 	    case catch ejabberd_odbc:sql_query(
 			 jlib:nameprep(Server),
-			 ["select password from users "
-			  "where username='", Username, "'"]) of
+			 ["EXECUTE dbo.get_password ""'", Username, "'"]) of
 		{selected, ["password"], [{Passwd}]} ->
 		    DigRes = if
 				 Digest /= "" ->
@@ -93,10 +91,7 @@
 	    Pass = ejabberd_odbc:escape(Password),
 	    catch ejabberd_odbc:sql_query(
 		    jlib:nameprep(Server),
-		    ["begin;"
-		     "delete from users where username='", Username ,"';"
-		     "insert into users(username, password) "
-		     "values ('", Username, "', '", Pass, "'); commit"])
+		    ["EXECUTE dbo. set_password ""'", Username, "', '", Pass, "'"])
     end.
 
 
@@ -109,8 +104,7 @@
 	    Pass = ejabberd_odbc:escape(Password),
 	    case catch ejabberd_odbc:sql_query(
 			 jlib:nameprep(Server),
-			 ["insert into users(username, password) "
-			  "values ('", Username, "', '", Pass, "')"]) of
+			 ["EXECUTE dbo.add_user ""'", Username, "', '", Pass, "'"]) of
 		{updated, _} ->
 		    {atomic, ok};
 		_ ->
@@ -124,7 +118,7 @@
 get_vh_registered_users(Server) ->
     case catch ejabberd_odbc:sql_query(
 		 jlib:nameprep(Server),
-		 "select username from users") of
+		 "EXECUTE dbo.list_users") of
 	{selected, ["username"], Res} ->
 	    [U || {U} <- Res];
 	_ ->
@@ -139,8 +133,7 @@
 	    Username = ejabberd_odbc:escape(LUser),
 	    case catch ejabberd_odbc:sql_query(
 			 jlib:nameprep(Server),
-			 ["select password from users "
-			  "where username='", Username, "'"]) of
+			 ["EXECUTE dbo.get_password ""'", Username, "'"]) of
 		{selected, ["password"], [{Password}]} ->
 		    Password;
 		_ ->
@@ -156,8 +149,7 @@
 	    Username = ejabberd_odbc:escape(LUser),
 	    case catch ejabberd_odbc:sql_query(
 			 jlib:nameprep(Server),
-			 ["select password from users "
-			  "where username='", Username, "'"]) of
+			 ["EXECUTE dbo.get_password ""'", Username, "'"]) of
 		{selected, ["password"], [{Password}]} ->
 		    Password;
 		_ ->
@@ -173,8 +165,7 @@
 	    Username = ejabberd_odbc:escape(LUser),
 	    case catch ejabberd_odbc:sql_query(
 			 jlib:nameprep(Server),
-			 ["select password from users "
-			  "where username='", Username, "'"]) of
+			 ["EXECUTE dbo.get_password ""'", Username, "'"]) of
 		{selected, ["password"], [{_Password}]} ->
 		    true;
 		_ ->
@@ -190,7 +181,7 @@
 	    Username = ejabberd_odbc:escape(LUser),
 	    catch ejabberd_odbc:sql_query(
 		    jlib:nameprep(Server),
-		    ["delete from users where username='", Username ,"'"]),
+		    ["EXECUTE dbo.del_user ""'", Username ,"'"]),
 	    ejabberd_hooks:run(remove_user, jlib:nameprep(Server), [User])
     end.
 
@@ -204,11 +195,8 @@
 	    case catch
 		ejabberd_odbc:sql_query(
 		  jlib:nameprep(Server),
-		  ["begin;"
-		   "select password from users where username='", Username, "';"
-		   "delete from users "
-		   "where username='", Username, "' and password='", Pass, "';"
-		   "commit"]) of
+		  ["begin transaction "
+		   "EXECUTE dbo.del_user_return_password ""'", Username, "'"]) of
 		{selected, ["password"], [{Password}]} ->
 		    ejabberd_hooks:run(remove_user, jlib:nameprep(Server),
 				       [User]),
diff -u mod_last_odbc.erl mod_last_odbc.erl
--- mod_last_odbc.erl	2005-11-22 12:57:15.000000000 +0100
+++ mod_last_odbc.erl	2005-11-22 12:59:48.000000000 +0100
@@ -98,8 +98,7 @@
     Username = ejabberd_odbc:escape(LUser),
     case catch ejabberd_odbc:sql_query(
 		 LServer,
-		 ["select seconds, state from last "
-		  "where username='", Username, "'"]) of
+		 ["EXECUTE dbo.get_last ""'", Username, "'"]) of
 	{'EXIT', _Reason} ->
 	    IQ#iq{type = error, sub_el = [SubEl, ?ERR_INTERNAL_SERVER_ERROR]};
 	{selected, ["seconds","state"], []} ->
@@ -136,11 +135,7 @@
     State = ejabberd_odbc:escape(Status),
     ejabberd_odbc:sql_query(
       LServer,
-      ["begin;"
-       "delete from last where username='", Username, "';"
-       "insert into last(username, seconds, state) "
-       "values ('", Username, "', '", Seconds, "', '", State, "');",
-       "commit"]).
+      ["EXECUTE dbo.set_last ""'", Username, "', '", Seconds, "', '", State, "'"]).
 
 
 remove_user(User, Server) ->
@@ -148,6 +143,5 @@
     LServer = jlib:nameprep(Server),
     Username = ejabberd_odbc:escape(LUser),
     ejabberd_odbc:sql_query(
-      LServer,
-      ["delete from last where username='", Username, "'"]).
-
+      LServer,      
+      ["EXECUTE dbo.del_last ""'", Username, "'"]).
diff -u mod_offline_odbc.erl mod_offline_odbc.erl
--- mod_offline_odbc.erl	2005-11-22 12:57:15.000000000 +0100
+++ mod_offline_odbc.erl	2005-11-22 13:00:12.000000000 +0100
@@ -68,14 +68,12 @@
 				  ejabberd_odbc:escape(
 				    lists:flatten(
 				      xml:element_to_string(Packet))),
-			      ["insert into spool(username, xml) "
-			       "values ('", Username, "', '",
-			       XML,
-			       "');"]
+
+			      ["EXECUTE dbo.add_spool ""'", Username, "'" " , " "'",XML,"'"]
 		      end, Msgs),
 	    case catch ejabberd_odbc:sql_query(
 			 Host,
-			 ["begin; ", Query, " commit"]) of
+			 [Query]) of
 		{'EXIT', Reason} ->
 		    ?ERROR_MSG("~p~n", [Reason]);
 		_ ->
@@ -213,14 +211,9 @@
     EUser = ejabberd_odbc:escape(LUser),
     case ejabberd_odbc:sql_query(
 	   LServer,
-	   ["begin;"
-	    "select * from spool where username='", EUser, "';"
-	    "delete from spool where username='", EUser, "';"
-	    "commit"]) of
-	[{updated, undefined},
-	 {selected, ["username","xml"], Rs},
-	 {updated, _},
-	 {updated, undefined}] ->
+	   ["EXECUTE dbo.get_and_del_spool_msg ""'", EUser, "'"]) of
+	[{selected, ["username","xml"], Rs},
+	 {updated, _}] ->
 	    Ls ++ lists:flatmap(
 		    fun({_, XML}) ->
 			    case xml_stream:parse_element(XML) of
@@ -251,5 +244,4 @@
     Username = ejabberd_odbc:escape(LUser),
     ejabberd_odbc:sql_query(
       LServer,
-      ["delete from spool where username='", Username, "'"]).
-
+      ["EXECUTE dbo.del_spool_msg ""'", Username, "'"]).
diff -u mod_roster_odbc.erl mod_roster_odbc.erl
--- mod_roster_odbc.erl	2005-11-22 12:57:15.000000000 +0100
+++ mod_roster_odbc.erl	2005-11-22 13:02:23.000000000 +0100
@@ -114,16 +114,13 @@
     Username = ejabberd_odbc:escape(LUser),
     case catch ejabberd_odbc:sql_query(
 		 LServer,
-		 ["select username, jid, nick, subscription, ask, "
-		  "server, subscribe, type from rosterusers "
-		  "where username='", Username, "'"]) of
+		 ["EXECUTE dbo.get_roster ""'", Username, "'"]) of
 	{selected, ["username", "jid", "nick", "subscription", "ask",
 		    "server", "subscribe", "type"],
 	 Items} when is_list(Items) ->
 	    JIDGroups = case catch ejabberd_odbc:sql_query(
 				     LServer,
-				     ["select jid, grp from rostergroups "
-				      "where username='", Username, "'"]) of
+				     ["EXECUTE dbo.get_roster_groups ""'", Username, "'"]) of
 			    {selected, ["jid","grp"],
 			     JGrps} when is_list(JGrps) ->
 				JGrps;
@@ -158,9 +155,7 @@
     Username = ejabberd_odbc:escape(LUser),
     case catch ejabberd_odbc:sql_query(
 		 LServer,
-		 ["select username, jid, nick, subscription, ask, "
-		  "server, subscribe, type from rosterusers "
-		  "where username='", Username, "'"]) of
+		 ["EXECUTE dbo.get_roster ""'", Username, "'"]) of
 	{selected, ["username", "jid", "nick", "subscription", "ask",
 		    "server", "subscribe", "type"],
 	 Items} when is_list(Items) ->
@@ -231,10 +226,7 @@
 	    SJID = ejabberd_odbc:escape(jlib:jid_to_string(LJID)),
 	    case catch ejabberd_odbc:sql_query(
 			 LServer,
-			 ["select username, jid, nick, subscription, ask, "
-			  "server, subscribe, type from rosterusers "
-			  "where username='", Username, "' "
-			  "and jid='", SJID, "'"]) of
+			 ["EXECUTE dbo.get_roster_by_jid ""'", Username, "'" " , " "'", SJID, "'"]) of
 		{selected, ["username", "jid", "nick", "subscription", "ask",
 			    "server", "subscribe", "type"],
 		 Res} ->
@@ -253,36 +245,12 @@
 			remove ->
 			    catch ejabberd_odbc:sql_query(
 				    LServer,
-				    ["begin;"
-				     "delete from rosterusers "
-				     "      where username='", Username, "' "
-				     "        and jid='", SJID, "';"
-				     "delete from rostergroups "
-				     "      where username='", Username, "' "
-				     "        and jid='", SJID, "';"
-				     "commit"]);
+				    ["EXECUTE dbo.del_roster ""'", Username, "'" " , " "'", SJID, "'"]);
 			_ ->
-			    ItemVals = record_to_string(Item2),
-			    ItemGroups = groups_to_string(Item2),
+			    ItemVals = record_to_string_without_group(Item2),
 			    catch ejabberd_odbc:sql_query(
 				    LServer,
-				    ["begin;"
-				     "delete from rosterusers "
-				     "      where username='", Username, "' "
-				     "        and jid='", SJID, "';"
-				     "insert into rosterusers("
-				     "              username, jid, nick, "
-				     "              subscription, ask, "
-				     "              server, subscribe, type) "
-				     " values ", ItemVals, ";"
-				     "delete from rostergroups "
-				     "      where username='", Username, "' "
-				     "        and jid='", SJID, "';",
-				     [["insert into rostergroups("
-				       "              username, jid, grp) "
-				       " values ", ItemGroup, ";"] ||
-					 ItemGroup <- ItemGroups],
-				     "commit"])
+				    ["EXECUTE dbo.add_roster_user ", ItemVals, " "])
 		    end,
 		    push_item(User, LServer, To, Item2),
 		    case Item2#roster.subscription of
@@ -414,9 +382,7 @@
     Username = ejabberd_odbc:escape(LUser),
     case catch ejabberd_odbc:sql_query(
 		 LServer,
-		 ["select username, jid, nick, subscription, ask, "
-		  "server, subscribe, type from rosterusers "
-		  "where username='", Username, "'"]) of
+		 ["EXECUTE dbo.get_roster ""'", Username, "'"]) of
 	{selected, ["username", "jid", "nick", "subscription", "ask",
 		    "server", "subscribe", "type"],
 	 Items} when is_list(Items) ->
@@ -425,7 +391,8 @@
 	    {[], []}
     end.
 
-fill_subscription_lists([I | Is], F, T) ->
+fill_subscription_lists([RawI | Is], F, T) ->
+    I = raw_to_record(RawI),
     J = I#roster.jid,
     case I#roster.subscription of
 	both ->
@@ -460,10 +427,7 @@
     SJID = ejabberd_odbc:escape(jlib:jid_to_string(LJID)),
     Item = case catch ejabberd_odbc:sql_query(
 			LServer,
-			["select username, jid, nick, subscription, ask, "
-			 "server, subscribe, type from rosterusers "
-			 "where username='", Username, "' "
-			 "and jid='", SJID, "'"]) of
+			["EXECUTE dbo.get_roster_by_jid ""'", Username, "'" " , " "'", SJID, "'"]) of
 	       {selected, ["username", "jid", "nick", "subscription", "ask",
 			   "server", "subscribe", "type"],
 		[I]} ->
@@ -496,19 +460,10 @@
 	       {Subscription, Pending} ->
 		   NewItem = Item#roster{subscription = Subscription,
 					 ask = Pending},
-		   ItemVals = record_to_string(NewItem),
+		   ItemVals = record_to_string_without_group(NewItem),
 		   catch ejabberd_odbc:sql_query(
 			   LServer,
-			   ["begin;"
-			    "delete from rosterusers "
-			    "      where username='", Username, "' "
-			    "        and jid='", SJID, "';"
-			    "insert into rosterusers("
-			    "              username, jid, nick, "
-			    "              subscription, ask, "
-			    "              server, subscribe, type) "
-			    " values ", ItemVals, ";"
-			    "commit"]),
+			   ["EXECUTE dbo.add_roster_user ", ItemVals, " "]),
 		   {push, NewItem}
 	   end,
     case AutoReply of
@@ -537,7 +492,6 @@
 
 in_state_change(none, none, subscribe)    -> {none, in};
 in_state_change(none, none, subscribed)   -> {to, none}; % Workaround for gateways
-%in_state_change(none, none, subscribed)   -> none;
 in_state_change(none, none, unsubscribe)  -> none;
 in_state_change(none, none, unsubscribed) -> none;
 in_state_change(none, out,  subscribe)    -> {none, both};
@@ -546,7 +500,6 @@
 in_state_change(none, out,  unsubscribed) -> {none, none};
 in_state_change(none, in,   subscribe)    -> none;
 in_state_change(none, in,   subscribed)   -> {to, in}; % Workaround for gateways
-%in_state_change(none, in,   subscribed)   -> none;
 in_state_change(none, in,   unsubscribe)  -> {none, none};
 in_state_change(none, in,   unsubscribed) -> none;
 in_state_change(none, both, subscribe)    -> none;
@@ -629,27 +582,20 @@
     Username = ejabberd_odbc:escape(LUser),
     catch ejabberd_odbc:sql_query(
 	    LServer,
-	    ["begin;"
-	     "delete from rosterusers "
-	     "      where username='", Username, "';"
-	     "delete from rostergroups "
-	     "      where username='", Username, "';"
-	     "commit"]),
+	    ["EXECUTE dbo.del_user_roster ""'", Username, "'"]),
     ok.
 
 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
-
+% TODOOOO
 set_items(User, Server, SubEl) ->
     {xmlelement, _Name, _Attrs, Els} = SubEl,
     LUser = jlib:nodeprep(User),
     LServer = jlib:nameprep(Server),
     catch ejabberd_odbc:sql_query(
 	    LServer,
-	    ["begin;",
-	     lists:map(fun(El) ->
+	    [lists:map(fun(El) ->
 			       process_item_set_t(LUser, LServer, El)
-		       end, Els),
-	     "commit"]).
+		       end, Els)]).
 
 process_item_set_t(LUser, LServer, {xmlelement, _Name, Attrs, Els}) ->
     JID1 = jlib:string_to_jid(xml:get_attr_s("jid", Attrs)),
@@ -667,30 +613,10 @@
 	    Item2 = process_item_els(Item1, Els),
 	    case Item2#roster.subscription of
 	        remove ->
-		    ["delete from rosterusers "
-		     "      where username='", Username, "' "
-		     "        and jid='", SJID, "';"
-		     "delete from rostergroups "
-		     "      where username='", Username, "' "
-		     "        and jid='", SJID, "';"];
+		    ["EXECUTE dbo.del_roster ""'", Username, "'" " , " "'", SJID, "'"];
 	        _ ->
-	            ItemVals = record_to_string(Item1),
-		    ItemGroups = groups_to_string(Item2),
-		    ["delete from rosterusers "
-		     "      where username='", Username, "' "
-		     "        and jid='", SJID, "';"
-		     "insert into rosterusers("
-		     "              username, jid, nick, "
-		     "              subscription, ask, "
-		     "              server, subscribe, type) "
-		     " values ", ItemVals, ";"
-		     "delete from rostergroups "
-		     "      where username='", Username, "' "
-		     "        and jid='", SJID, "';",
-		     [["insert into rostergroups("
-		       "              username, jid, grp) "
-		       " values ", ItemGroup, ";"] ||
-			 ItemGroup <- ItemGroups]]
+	            ItemVals = record_to_string_without_group(Item1),
+                    ["EXECUTE dbo.add_roster_user ", ItemVals, " "]
 	    end
     end;
 process_item_set_t(_LUser, _LServer, _) ->
@@ -747,9 +673,7 @@
     SJID = ejabberd_odbc:escape(jlib:jid_to_string(LJID)),
     case catch ejabberd_odbc:sql_query(
 		 LServer,
-		 ["select subscription from rosterusers "
-		  "where username='", Username, "' "
-		  "and jid='", SJID, "'"]) of
+		 ["EXECUTE dbo.get_subscription ""'", Username, "'" " , " "'", SJID, "'"]) of
 	{selected, ["subscription"], [{SSubscription}]} ->
 	    Subscription = case SSubscription of
 			       "B" -> both;
@@ -759,9 +683,7 @@
 			   end,
 	    Groups = case catch ejabberd_odbc:sql_query(
 				  LServer,
-				  ["select grp from rostergroups "
-				   "where username='", Username, "' "
-				   "and jid='", SJID, "'"]) of
+				  ["EXECUTE dbo.get_rostergroup_by_jid ""'", Username, "'" " , " "'", SJID, "'"]) of
 			 {selected, ["grp"], JGrps} when is_list(JGrps) ->
 			     JGrps;
 			 _ ->
@@ -777,9 +699,7 @@
 		    SRJID = ejabberd_odbc:escape(jlib:jid_to_string(LRJID)),
 		    case catch ejabberd_odbc:sql_query(
 				 LServer,
-				 ["select subscription from rosterusers "
-				  "where username='", Username, "' "
-				  "and jid='", SRJID, "'"]) of
+				 ["EXECUTE dbo.get_subscription ""'", Username, "'" " , " "'", SRJID, "'"]) of
 			{selected, ["subscription"], [{SSubscription}]} ->
 			    Subscription = case SSubscription of
 					       "B" -> both;
@@ -789,9 +709,7 @@
 					   end,
 			    Groups = case catch ejabberd_odbc:sql_query(
 						  LServer,
-						  ["select grp from rostergroups "
-						   "where username='", Username, "' "
-						   "and jid='", SRJID, "'"]) of
+						  ["EXECUTE dbo.get_rostergroup_by_jid ""'", Username, "'" " , " "'", SRJID, "'"]) of
 					 {selected, ["grp"], JGrps} when is_list(JGrps) ->
 					     JGrps;
 					 _ ->
@@ -834,11 +752,12 @@
 		    ask = Ask}
     end.
 
-record_to_string(#roster{user = User,
+record_to_string_with_group(#roster{user = User,
 			 jid = JID,
 			 name = Name,
 			 subscription = Subscription,
-			 ask = Ask}) ->
+			 ask = Ask,
+                         groups = Groups}) ->
     Username = ejabberd_odbc:escape(User),
     SJID = ejabberd_odbc:escape(jlib:jid_to_string(JID)),
     Nick = ejabberd_odbc:escape(Name),
@@ -856,21 +775,41 @@
 	       in	   -> "I";
 	       none	   -> "N"
 	   end,
-    ["("
+    [[""
      "'", Username, "',"
      "'", SJID, "',"
      "'", Nick, "',"
      "'", SSubscription, "',"
      "'", SAsk, "',"
-     "'N', '', 'item')"].
+     "'N', '', 'item',"
+     "'", ejabberd_odbc:escape(Group), "'"] || Group <- Groups].
 
-groups_to_string(#roster{user = User,
+record_to_string_without_group(#roster{user = User,
 			 jid = JID,
-			 groups = Groups}) ->
+			 name = Name,
+			 subscription = Subscription,
+			 ask = Ask}) ->
     Username = ejabberd_odbc:escape(User),
     SJID = ejabberd_odbc:escape(jlib:jid_to_string(JID)),
-    [["("
-      "'", Username, "',"
-      "'", SJID, "',"
-      "'", ejabberd_odbc:escape(Group), "')"] || Group <- Groups].
-
+    Nick = ejabberd_odbc:escape(Name),
+    SSubscription = case Subscription of
+			both -> "B";
+			to   -> "T";
+			from -> "F";
+			none -> "N"
+		    end,
+    SAsk = case Ask of
+	       subscribe   -> "S";
+	       unsubscribe -> "U";
+	       both	   -> "B";
+	       out	   -> "O";
+	       in	   -> "I";
+	       none	   -> "N"
+	   end,
+    [""
+     "'", Username, "',"
+     "'", SJID, "',"
+     "'", Nick, "',"
+     "'", SSubscription, "',"
+     "'", SAsk, "',"
+     "'N', '', 'item'"].
