Showing posts with label single. Show all posts
Showing posts with label single. Show all posts

Monday, March 12, 2012

Function to create comma separated list from any given column/table.

Hi,

I'm sure this is a common problem.. to create a single field from a
whole column, where each row would be separated by a comma.

I can do this for a specified table, and column.. and I've created a
function using VBA to achieve a more dynamic (and very slow) solution..
so I would like to implement it using a user defined function in sql server.

The problems I'm facing are, that I can't use dynamic sql in a
function.. and I also can't use temporary tables which could build up a
'standard' table from parameters given to then perform the function on.

So, with these limitations, what other options do I have?

Cheers,
ChrisDynamic SQL is so so passe'

Firstly it is a security risk , as you can't lock down rights to procedures
that use it.

secondly it is inefficient as it has to be re-generated and compiled each
time you use it

Code Generation is the all the rage now.

And how often will you be adding new code tables to do this with?

Here's how to roll your own code generator in SQL:

in Query Analyzer (aka ISQL)

write a template code block to get it to work (I've done this below)

wrap that in print statements.

create a cursor to loop thru sysobjects and/or syscolumns (or the SCHEMA
Views) to locate all your tables with the columns you need.

loop thru and generate all the script you need.

you can create a separate function/procedure for each or create a single one
with a large case in it (your call).

run the output to create the function(s).

save the script that generates the stuff and hold on to it.

every time a change is made to a code table (i.e. add new table, change
column names, etc..) rerun the script to regenerated (make sure there is a
drop in there somewhere as well)

Tally Ho! - (which is a liquor store her in Delaware, and not a Fox hunting
term, which I understand is now outlawed in the UK).

------------------
-- declare an empty string (not null)
declare @.list varchar 4000
set @.list = ''

-- select multiple rows into a single variable
select @.list = @.list + ',' + ColumnName from TableName

-- get rid of the last comma
select @.list = substring( @.list , 1, len( @.list ) -1 )
------------------

"Not Me" <Noone.is.home@.here.com> wrote in message
news:cl87ou$fam$1@.ucsnew1.ncl.ac.uk...
> Hi,
> I'm sure this is a common problem.. to create a single field from a whole
> column, where each row would be separated by a comma.
> I can do this for a specified table, and column.. and I've created a
> function using VBA to achieve a more dynamic (and very slow) solution.. so
> I would like to implement it using a user defined function in sql server.
> The problems I'm facing are, that I can't use dynamic sql in a function..
> and I also can't use temporary tables which could build up a 'standard'
> table from parameters given to then perform the function on.
> So, with these limitations, what other options do I have?
> Cheers,
> Chris

Function that returns a table

I have a function that returns a single row table with two columns:

dbo.Fun1(@.param1) : colA and colB

I tried to create a stored procedure that use this function:

select col1, col2, dbo.Fun1(col1) from table1

The result is : Invalid object name 'dbo.Fun1'

There is no join between table1 and Fun1, how can I select the both columns of Fun1 ?

Thanks in advance.

Long

Can u paste the declaration of the function?|||You are trying to use a table-valued UDF like a scalar UDF which is incorrect. You can use a table-valued function only in the FROM clause or as a table source. In any case, what you are trying to do is not possible in SQL Server 2000 since you can only pass variables or constants as parameters to table-valued UDFs. In SQL Server 2005, you can use the APPLY operator to the same.|||

Thanks, Umachandar,

I have to do the selection like this:

select col1, col2, (select colA from dbo.Fun1(col1) ), ( select colB from dbo.Fun1(col1))

from table1

It works, but I'm not satisfied, as it calculates the function twice.

Any other ideas?

Thanks in advance.

Long

|||

Hi,
due to the fact that you have to execute the statement once per row, there is no way to do it ohter than your mentioned way.

Without knowing your Function I would assume that even this is very wacky, because your Return could return more than one value ?! So you have to make sure from your query / ir function that only one row will be returned.

HTH, Jens Suessmeyer.

|||

I don't see how this will work in SQL2000. If you are on SQL2005 then you can simplify the query by using APPLY operator like:

select t.col1, t.col2, f.colA, f.colB

from table1 as t

cross apply dbo.Fun1(t.col1) as f

Function similar to ISNULL()

I'm constructing a single string of several counts with concatenated labels using SQL and want to not show zeros (or their labels). Is there a function within an SQL statement that will let me do this? ISNULL() sort of does this, but I really need to test for zero instead of NULL to eliminate noise data from the string.You could write your own User Defined Function. Check out Books On Line for more info.
|||


Try the links below you may find T-SQL funtion to do what you need, but remember ISNULL can give just plain wrong numbers with OUTER JOIN in SQL Server, it is a known issue. Hope this helps.

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/tsqlref/ts_nos-nz_3uhy.asp

http://www.samspublishing.com/articles/article.asp?p=31283&seqNum=3&rl=1

Sunday, February 26, 2012

Full-text search: Problem with single/double quotes

Hi,
I am developing Knowledge Database(intranet) for my compnay.
I am using SQL Server 2000 as DB and Active Server Pages as Scripting
language.
I have a table, tb_kdb with 3 fields(title,keyword,description) as text
type.
I am insering the form data into DB after encoding.for e.g
rs("title") = Server.HTMLEncode(Request.Form("txtTitle"))
I have enabled Full-text search for these 3
fields(title,keyword,description).
Now whenever i enter data in the HTML form as - Error code: "H2SCX" in the
configuration file- for tile field.
This will be stored in the DB field (title) as - Error code:
"H2SCX" in the configuration file.
After this i do "start Full Population"
But when i try to search string - H2SCX from title field i will get 0
records found.Actually i should get 1 record found.
If i remove quotes around string - H2SCX and do again Full population and
seach, i get 1 record found.
I feel the problem is due to te quotes around string. I am facing this
problem for single & double quotes.
There are number of FAQs on our intranet site which has single or double
quotes...when searched those records will not be diaplayed.
I tried the below queriy on Sql Query Analyser
SELECT title FROM tb_kdb WHERE CONTAINS(title,'H2SCX')
SELECT @.@.Version output:
Microsoft SQL Server 2000 - 8.00.818 (Intel X86) May 31 2003 16:08:15
Copyright (c) 1988-2003 Microsoft Corporation Standard Edition on Windows
NT 5.0 (Build 2195: Service Pack 4)
Any help or suggestion on this is higly appreciated.
Thanks & Best Rgds,
Surjit
replace your single quotes with two single quotes, and your double quotes
with two double quotes.
"Surjit Madiwalar" <sursatraj@.hotmail.com> wrote in message
news:ua3qw78FEHA.684@.tk2msftngp13.phx.gbl...
> Hi,
> I am developing Knowledge Database(intranet) for my compnay.
> I am using SQL Server 2000 as DB and Active Server Pages as Scripting
> language.
> I have a table, tb_kdb with 3 fields(title,keyword,description) as text
> type.
> I am insering the form data into DB after encoding.for e.g
> rs("title") = Server.HTMLEncode(Request.Form("txtTitle"))
> I have enabled Full-text search for these 3
> fields(title,keyword,description).
> Now whenever i enter data in the HTML form as - Error code: "H2SCX" in the
> configuration file- for tile field.
> This will be stored in the DB field (title) as - Error code:
> "H2SCX" in the configuration file.
> After this i do "start Full Population"
> But when i try to search string - H2SCX from title field i will get 0
> records found.Actually i should get 1 record found.
> If i remove quotes around string - H2SCX and do again Full population and
> seach, i get 1 record found.
> I feel the problem is due to te quotes around string. I am facing this
> problem for single & double quotes.
> There are number of FAQs on our intranet site which has single or double
> quotes...when searched those records will not be diaplayed.
> I tried the below queriy on Sql Query Analyser
> SELECT title FROM tb_kdb WHERE CONTAINS(title,'H2SCX')
> SELECT @.@.Version output:
> Microsoft SQL Server 2000 - 8.00.818 (Intel X86) May 31 2003 16:08:15
> Copyright (c) 1988-2003 Microsoft Corporation Standard Edition on Windows
> NT 5.0 (Build 2195: Service Pack 4)
>
> Any help or suggestion on this is higly appreciated.
> Thanks & Best Rgds,
> Surjit
>
>
>

Full-Text Search stops working

Hi There
I have a single server with Win 2000 Server and SQL 2000 SP3a.
I have a database with 1 full-text index, and everything works fine.....
BUT!!
Suddenly the search stops working. There are no errors in the eventlog and
all services are running fine.
The only way I can start the search, is to reboot the server !!
Whats wrong !?!?
/Peter
are there any messages in the gatherer logs?
"Microsoft" <pemaz@.wmdata.dk> wrote in message
news:%23hGeHL7TFHA.2124@.TK2MSFTNGP14.phx.gbl...
> Hi There
> I have a single server with Win 2000 Server and SQL 2000 SP3a.
> I have a database with 1 full-text index, and everything works fine.....
> BUT!!
> Suddenly the search stops working. There are no errors in the eventlog and
> all services are running fine.
> The only way I can start the search, is to reboot the server !!
> Whats wrong !?!?
> /Peter
>
|||No, there are no errors in the gatherer logs.
The index is only populated once.
/Peter
Hilary Cotter wrote:

> 220 0 article <uItFbR#TFHA.2664@.TK2MSFTNGP15.phx.gbl>
> From: "Hilary Cotter" <hilary.cotter@.gmail.com>
> References: <#hGeHL7TFHA.2124@.TK2MSFTNGP14.phx.gbl>
> Subject: Re: Full-Text Search stops working
> Date: Tue, 3 May 2005 09:28:58 -0400
> Lines: 23
> X-Priority: 3
> X-MSMail-Priority: Normal
> X-Newsreader: Microsoft Outlook Express 6.00.2900.2180
> X-MIMEOLE: Produced By Microsoft MimeOLE V6.00.2900.2180
> X-RFC2646: Format=Flowed; Original
> Message-ID: <uItFbR#TFHA.2664@.TK2MSFTNGP15.phx.gbl>
> Newsgroups: microsoft.public.sqlserver.fulltext
> NNTP-Posting-Host: ool-4353c4df.dyn.optonline.net 67.83.196.223
> Path: TK2MSFTNGP08.phx.gbl!TK2MSFTNGP15.phx.gbl
> Xref: TK2MSFTNGP08.phx.gbl microsoft.public.sqlserver.fulltext:14771
>
> are there any messages in the gatherer logs?
> "Microsoft" <pemaz@.wmdata.dk> wrote in message
> news:%23hGeHL7TFHA.2124@.TK2MSFTNGP14.phx.gbl...
>
|||I have checked the gather logs and theres nothing "fishy"
5/3/2005 9:22:16 AM Add The gatherer has started
5/3/2005 9:22:18 AM Add The initialization has completed
5/3/2005 12:40:48 PM Add The gatherer has started
5/3/2005 12:40:50 PM Add The initialization has completed
5/3/2005 1:13:10 PM Add The gatherer has started
5/3/2005 1:13:12 PM Add The initialization has completed
Peter Mazzarella wrote:

> No, there are no errors in the gatherer logs.
> The index is only populated once.
> /Peter
>
>
>
>
>
> Hilary Cotter wrote:
>
|||What happens if you stop and start MSSearch after your search functionality
stops?
Hilary Cotter
Looking for a SQL Server replication book?
http://www.nwsu.com/0974973602.html
Looking for a FAQ on Indexing Services/SQL FTS
http://www.indexserverfaq.com
"Peter Mazzarella" <pemaz@.wmdata.dk> wrote in message
news:uB2oNUJUFHA.1040@.TK2MSFTNGP10.phx.gbl...
> No, there are no errors in the gatherer logs.
> The index is only populated once.
> /Peter
>
>
>
>
>
> Hilary Cotter wrote:
>