all groups > sql server programming > february 2005 >
You're in the

sql server programming

group:

user defined function and syntax question


user defined function and syntax question nicholas gadacz
2/20/2005 9:21:03 PM
sql server programming:
hello all,

I am tring to build a form building tool inside the database. I know most of
you will say put it in the application layer, but I would like to keep this
text formatting in the database so I only have to write the function once and
our 2 unix server, and one windows server can all access the same code to
draw the form elements.

below it my function so far, i cannot get the syntax correct do do a select
in a Case statement. Is this possible in t-SQL?

-- function
IF EXISTS (SELECT *
FROM sysobjects
WHERE name = N'fn_draw_form_element')
DROP FUNCTION fn_draw_form_element
GO

CREATE FUNCTION fn_draw_form_element ( @id_form_detail int)

RETURNS varchar(8000)
AS
Begin

declare
@element_start varchar(50),
@element_middle varchar(2000),
@element_end varchar(50),

@element_final varchar(2100),
@detail_type varchar(50)


-- find out what type of element it is
select @detail_type = detail_type
from form_detail with (nolock)
where id_form_detail = @id_form_detail

-- select the form in three parts
select
@element_start =
case @detail_type
when 'select' then '<select name="' + cast(@id_form_detail as
varchar(10)) + '">'
end,

@element_middle =
case @detail_type
when 'select' then '<option value=' + coalesce(@element_middle+' '+
char(13) + char(10),'') + element_name from (select element_name from
form_element with (nolock) where id_form_detail = @id_form_detail)
element_name + '">'
end
-- select @element_display


set @element_final = @element_start + @element_middle

return @element_final

End

Go

-- related tables
if exists (select * from dbo.sysobjects where id =
object_id(N'[dbo].[FK_form_elements_form_detail]') and OBJECTPROPERTY(id,
N'IsForeignKey') = 1)
ALTER TABLE [dbo].[form_element] DROP CONSTRAINT FK_form_elements_form_detail
GO

if exists (select * from dbo.sysobjects where id =
object_id(N'[dbo].[FK_form_detail_form_main]') and OBJECTPROPERTY(id,
N'IsForeignKey') = 1)
ALTER TABLE [dbo].[form_detail] DROP CONSTRAINT FK_form_detail_form_main
GO

if exists (select * from dbo.sysobjects where id =
object_id(N'[dbo].[form_detail]') and OBJECTPROPERTY(id, N'IsUserTable') = 1)
drop table [dbo].[form_detail]
GO

if exists (select * from dbo.sysobjects where id =
object_id(N'[dbo].[form_element]') and OBJECTPROPERTY(id, N'IsUserTable') = 1)
drop table [dbo].[form_element]
GO

if exists (select * from dbo.sysobjects where id =
object_id(N'[dbo].[form_main]') and OBJECTPROPERTY(id, N'IsUserTable') = 1)
drop table [dbo].[form_main]
GO

CREATE TABLE [dbo].[form_detail] (
[id_form_detail] [int] IDENTITY (1, 1) NOT NULL ,
[id_form_main] [int] NOT NULL ,
[detail_name] [varchar] (50) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL ,
[detail_type] [varchar] (50) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL ,
[required] [int] NOT NULL ,
[order_by] [int] NOT NULL
) ON [PRIMARY]
GO

CREATE TABLE [dbo].[form_element] (
[id_form_element] [int] IDENTITY (1, 1) NOT NULL ,
[id_form_detail] [int] NOT NULL ,
[element_name] [varchar] (50) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL ,
[order_by] [int] NOT NULL
) ON [PRIMARY]
GO

CREATE TABLE [dbo].[form_main] (
[id_form_main] [int] IDENTITY (1, 1) NOT NULL ,
[id_agency] [int] NOT NULL ,
[form_title] [varchar] (50) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL ,
[is_emailed] [int] NOT NULL
) ON [PRIMARY]
GO



Re: user defined function and syntax question Dave Frommer
2/21/2005 12:40:03 AM
No, the Case in SQL is a Case expression, not a case statement and you can
not embed selects in it.

"nicholas gadacz" <nicholasgadacz@discussions.microsoft.com> wrote in
message news:3F9ABF5D-1C00-4806-A468-F75FFDAFD962@microsoft.com...
[quoted text, click to view]

Re: user defined function and syntax question Uri Dimant
2/21/2005 7:43:42 AM
Hi
Try this
CREATE FUNCTION fn_draw_form_element ( @id_form_detail int)

RETURNS varchar(8000)
AS
Begin

declare
@element_start varchar(50),
@element_middle varchar(2000),
@element_end varchar(50),
@element_name varchar(50),

@element_final varchar(2100),
@detail_type varchar(50)


-- find out what type of element it is
select @detail_type = detail_type
from form_detail with (nolock)
where id_form_detail = @id_form_detail

select @element_name=element_name from -----I removed it from CASE
expresion.
form_element with (nolock) where id_form_detail = @id_form_detail


-- select the form in three parts
select
@element_start =
case @detail_type
when 'select' then '<select name="' + cast(@id_form_detail as
varchar(10)) + '">'
end,
@element_middle =
case @detail_type
when 'select' then '<option value=' + coalesce(@element_middle+' '+
char(13) + char(10),'') + coalesce(@element_name ,'')+'">'
end
-- select @element_display


set @element_final = @element_start + @element_middle

return @element_final

End

Go
drop function fn_draw_form_element





"nicholas gadacz" <nicholasgadacz@discussions.microsoft.com> wrote in
message news:3F9ABF5D-1C00-4806-A468-F75FFDAFD962@microsoft.com...
[quoted text, click to view]

Re: user defined function and syntax question Robbe Morris [C# MVP]
2/21/2005 8:44:56 AM
I understand your goal of only writing something once. However,
SQL Server is incredibly inefficient at string concatenation. This is
a not a good design decision on your part.

--
2005 Microsoft MVP C#
Robbe Morris
http://www.robbemorris.com
http://www.mastervb.net/home/ng/forumvbcode/post10017013.aspx
http://www.eggheadcafe.com/articles/adonet_source_code_generator.asp



"nicholas gadacz" <nicholasgadacz@discussions.microsoft.com> wrote in
message news:3F9ABF5D-1C00-4806-A468-F75FFDAFD962@microsoft.com...
[quoted text, click to view]

AddThis Social Bookmark Button