Solved

a single SQL proc for crud with one xml param?

Posted on 2010-08-12
12
472 Views
Last Modified: 2012-05-10
i was looking at making a bunch of procs to do simple CRUD operations in the DB and wanted to look into using XML as a param instead of separate variables so that i can have the UI just crank out these for various tables and maybe pass the table name to the proc along with the values

can anyone post some sample code on how to do this for lets say an employee table with empID, empName, age fields.
0
Comment
Question by:realcoding
  • 5
  • 3
  • 2
  • +1
12 Comments
 
LVL 60

Accepted Solution

by:
chapmandew earned 400 total points
ID: 33423527
Great idea, actually....you'll need to parse the xml, but other than that, you should be all good


create proc myproc
(      
      @x xml
)
as
begin
      insert into tablename(fld1, fld2)
      select c.value('@fld1', 'int'),
      c.value('@fld2', 'varchar(50)')
      from @x.nodes('/params') t(c)
end
go

declare @x xml
set @x = '<params fld1 = "3" fld2 = "timchapman"></params>'

exec myproc @x
0
 

Author Comment

by:realcoding
ID: 33423878
im trying to do all crud in one proc like this...

the sample works well but failed when i made the update to pull 1 more param for operation
create proc myCRUDproc
( @x xml)
as
begin

declare @operation varchar(50)
set @operation = select c.value('@operation','varchar(50)') from @x.nodes('/params') t(c)

IF @operation = 'ins'
BEGIN
	print 'inserting data'
END      

IF @operation = 'del'
BEGIN
	print 'deleting data'
END    

IF @operation = 'upd'
BEGIN
	print 'updating data'
END    	

declare @x xml
set @x = '<params operation="ins" SCID="3" Name="mike" Description="xml testing"></params>'
exec myproc @x

Open in new window

0
 

Author Comment

by:realcoding
ID: 33424321
i got it working just fails on the check to see if the xml is empty or doesn't exist so i do a select. how do i check for this... see code for where i do a select please.
create proc crudMilestone
( @x xml = '')
as

IF @x = ''
BEGIN
	select * from StatusCodeGroup2
END

declare @operation varchar(50)
set @operation = (select c.value('@operation','varchar(50)') from @x.nodes('/params') t(c))

IF @operation = 'ins'
BEGIN
	print 'inserting data'
END      

IF @operation = 'del'
BEGIN
	print 'deleting data'
END    

IF @operation = 'upd'
BEGIN
	print 'updating data'
END    	


declare @x xml
set @x = '<params operation="ins" SCID="3" Name="mike" Description="xml testing"></params>'
exec crudMilestone @x

Open in new window

0
Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

 
LVL 60

Expert Comment

by:chapmandew
ID: 33424688
you just want to check to see if the input param is null?
0
 

Author Comment

by:realcoding
ID: 33424707
null or empty, yes.
0
 
LVL 60

Expert Comment

by:chapmandew
ID: 33424717
will you always supply a param?
0
 

Author Comment

by:realcoding
ID: 33424753
not always, i want to handle all cases... who knows what those nutty devs will be doing.
0
 

Author Comment

by:realcoding
ID: 33424763
i am using this for now but dont know if its good or not...

IF(@x IS NULL or cast(@x as varchar(max)) ='')
Begin
...
End
0
 
LVL 75

Expert Comment

by:Anthony Perkins
ID: 33425960
You cannot pass a "" to an Xml it is not a well-formed document, so there is no need to check for that.  Just NULLs (and personally I would not even check for that, what is the point, eitehr way the client is going to get an error).
0
 
LVL 58

Assisted Solution

by:cyberkiwi
cyberkiwi earned 100 total points
ID: 33426263
anthony,

give this a try

------------------

create proc testbadxml @xml xml as
select n.c.query('.') from @xml.nodes('/') n(c)
GO

declare @xml xml
set @xml = ''
exec testbadxml @xml
0
 
LVL 58

Expert Comment

by:cyberkiwi
ID: 33426273

create proc crudMilestone
( @x xml = null)      --- << I changed, strictly speaking '' is not valid xml
as

IF nullif(convert(varchar(max),@x),'') is null
BEGIN
	select * from StatusCodeGroup2
END
......

Open in new window

0
 
LVL 75

Expert Comment

by:Anthony Perkins
ID: 33426323
You are right.  I stand corrected.
0

Featured Post

Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The Confluence of Individual Knowledge and the Collective Intelligence At this writing (summer 2013) the term API (http://dictionary.reference.com/browse/API?s=t) has made its way into the popular lexicon of the English language.  A few years ago, …
In this article I will describe the Backup & Restore method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
This video shows, step by step, how to configure Oracle Heterogeneous Services via the Generic Gateway Agent in order to make a connection from an Oracle session and access a remote SQL Server database table.
Viewers will learn how to use the INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.

808 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question