Showing posts with label string. Show all posts
Showing posts with label string. Show all posts

Friday, March 30, 2012

How to construct the query in Stored Proc

Hi,
In the past I build up the query string within VB.NET page and easy to add the filtering statements in the SQL statement since it is just a string. For example, if user selected an option then include it in the filter, otherwise, just return all rows from table:
Codes in ASPX.VB:
Dim SQL as String="SELECT * From Table1"
IF UserOption <> Null then
SQL = SQL & " WHERE Column1=" UserOption"
End if

Now, since I have a complicated page which need to use Stored Procedure to manapulate a temporary table before the final result. But I found when I want to add some user options similar to above, I found I don't know how to do it in Stored Procedure. In the Stored Procedure Property screen, I can't insert a IF..THEN statement within a SELECT statement. Seems I can only check the user option first and then determine the SELECT statement to use. That is: IF UserOption THEN SELECT statement 1 ELSE SELECT statement 2.
But it is impossible for me to do this way since I'm not only one user option on the page. User usually can have several filters/selections on his screen. So if check which user option(s) are selected and write a static SELECT statement for it, I will have to program a complicated store procedure to cater all combinations for all user options (where some options may be null).
Hope you can understanding what my mean and give me advices.
Regards,
Raymond

Hi,
you can use in Store Proc query dinamic creation :
CREATE PROCEDURE XXXX

@.OPTION1 INT

AS

DECLARE @.MYQUERY VARCHAR(1000)
DECLARE @.MYFILTER VARCHAR(1000)

SET @.MYQUERY = 'SELECT * FROM Table1 WHERE WHERE ID <> 0'

IF @.OPTION1 <> ''
BEGIN
SET @.MYFILTER = ' AND NegozioNome = ' + CONVERT(VARCHAR(20),@.OPTION1)
END

EXEC (@.MYQUERY + @.MYFILTER)

|||

I n SQL Server you can use the CASE statement, try the links below for sample code. Hope this helps.
http://www.craigsmullins.com/ssu_0899.htm

http://www.4guysfromrolla.com/webtech/102704-1.shtml

|||

Thanks a lot! I will study it. I also find this structure may help, which introducedd in another thread in this forum:

AND ([DD1 Industry Code] LIKE @.FilterIndustries OR @.FilterIndustries ='')

|||

scu wrote:

Hi,
you can use in Store Proc query dinamic creation :
CREATE PROCEDURE XXXX

@.OPTION1 INT

AS

DECLARE @.MYQUERY VARCHAR(1000)
DECLARE @.MYFILTER VARCHAR(1000)

SET @.MYQUERY = 'SELECT * FROM Table1 WHERE WHERE ID <> 0'

IF @.OPTION1 <> ''
BEGIN
SET @.MYFILTER = ' AND NegozioNome = ' + CONVERT(VARCHAR(20),@.OPTION1)
END

EXEC (@.MYQUERY + @.MYFILTER)


Will it affect the performance when the query is built up in a dynamic string rather than build up within stored procedure using CASE/IF..THEN..ELSE/COALESCE statements? It is important concern to the effectiveness when the store procedure contains complicated queries.|||Hi,
I've used many times dinamic queries in Store Proc.
The performance's difference between dinamic queries and equivalent 'static' storec proc is imperceptible.
|||

scu wrote:

Hi,
I've used many times dinamic queries in Store Proc.
The performance's difference between dinamic queries and equivalent 'static' storec proc is imperceptible.



Thanks for the kindly sharing from all of you!|||You will get better performance out of a dynamic SQL statement than you would using CASE statements.
See this link for more information on dynamic T-SQL and different ways to handle your query:
http://www.sommarskog.se/dyn-search.html

Nick|||

nick-w wrote:

You will get better performance out of a dynamic SQL statement than you would using CASE statements.
See this link for more information on dynamic T-SQL and different ways to handle your query:
http://www.sommarskog.se/dyn-search.html

Nick

Great article! Thanks Nick!

Wednesday, March 28, 2012

How to construct a like compariso when there are special characts

How do I construct a like compariso query when there are special characts
such as [ and ] in the string, i.e. [Y] to be searched? Thanks.
--
bicbic,
use a scape character.
Example:
select *
from
(
select 'abc[Y]def' as c1
union all
select 'abc[Y]def' as c1
) as t1
where c1 like '%\[Y\]%' escape ''
go
AMB
"bic" wrote:

> How do I construct a like compariso query when there are special characts
> such as [ and ] in the string, i.e. [Y] to be searched? Thanks.
> --
> bic|||You can use the ESCAPE clause. In this example I'm using '!' as the escape
character:
DECLARE @.x VARCHAR(100)
SELECT @.x = '[Y]'
SELECT
CASE
WHEN @.x LIKE '![Y!]' ESCAPE '!' THEN 'Match'
ELSE 'No Match' END AS Answer
"bic" <bic@.discussions.microsoft.com> wrote in message
news:B548D713-0022-47D1-A0E4-47630561159C@.microsoft.com...
> How do I construct a like compariso query when there are special characts
> such as [ and ] in the string, i.e. [Y] to be searched? Thanks.
> --
> bic

How to connect, after all ?

Hi All,

i have a problem, so lets to it.

Situation:

I have a COM that connects to SQL2K with that string connection:
"Provider=SQLOLEDB;Persist Security Info = False;DATABASE=MyDatabase;SERVER=MyServer;User Id=myuser;Password=123456;"

that works fine, but we have a problem: the person that compiles de DLL knows user and password, so we decided to change the way the application connects to SQL, and start using AppRoles.

Things we did:
1-I created an approle and gave the permissions to the role.

2-I grant connect permission to domain\user1, only connect, that will be used when registering the COM as domain\user responsible to be used to stablish the connection.

3-Developers changed string connection to: "Provider='SQLOLEDB';Data Source='MyServer';Initial Catalog='Mydatabase';Integrated Security='SSPI';"

New behavior:

The connection goes OK, but the way we did before, when traced with profiler, the sp_reset_connection has been invoked by sqlserver, and with the knew connect string was no more executed.

Another problem is that when the COM is called in a second time, the previous connection is logged out and it stops, not connecting again.

Any ideas ??

thanks a lot.

Leandro.What's the source code of sp_reset_connection ( are you sure this is a standard MS sp)|||i dont know, sp_reset_connection is invoked by sqlserver that controls pooling connection.

the fact is that with the string connection used before the sp_reset... has been invoked, and with the new configuration was no more.

we setup a domain\user responsible to the COM and granted connect permission to it and the code of the COM is executing sp_setapprole.

the first time the COM is called it works fine, but the second time the sp_reset_connection is not invoked, and yhe connection is logged out.

the third time the COM is invoked all works fine again, when i trace the connection the select runs OK, ando so on.

any ideas ?|||I would try adding "Persist Security Info = False;" back into your connection string

Monday, March 26, 2012

How to connect to remote MSDE database using VB.NET

Greetings
I have a VB.NET application that works great with a local MSDE database. But, when I try to revise my connection string to point to a remote MSDE server and database, it fails. The MSDE Server Manager can pick up the remote server and tell me its runni
ng, but when I try to register it using SQL Server, or use a DSN to connect to it, I get a "SQL Server does not exist or access denied. ConnectionOpen(Connect())" My connection string is:
Provider=SQLOLEDB.1;Server=DSS\VSDOTNET;Database=d atabase;User ID=sa;Password = xxxxx;Integrated Security=SSPI
Any help will be appreciated. Thanks...
- Tim B.
hi Tim
"Tim Balderramos" <pacmantab@.hotmail.com> ha scritto nel messaggio
news:28F08CF8-86CF-4C04-9E5A-900296EC78A7@.microsoft.com...
> Greetings
> I have a VB.NET application that works great with a local MSDE database.
But, when I try
>to revise my connection string to point to a remote MSDE server and
database, it fails. The
>MSDE Server Manager can pick up the remote server and tell me its running,
but when I try
>to register it using SQL Server, or use a DSN to connect to it, I get a
"SQL Server does not
>exist or access denied. ConnectionOpen(Connect())" My connection string
is:
> Provider=SQLOLEDB.1;Server=DSS\VSDOTNET;Database=d atabase;User
>ID=sa;Password = xxxxx;Integrated Security=SSPI
the most frequent causes of that kind of error are reported in
http://support.microsoft.com/default...06&Product=sql
KB article...
please verify the remote MSDE instance has enabled networkprotocols too,
using the remote Server Network Utilityes (svrnecn.exe)
Andrea Montanari (Microsoft MVP - SQL Server)
http://www.asql.biz/DbaMgr.shtmhttp://italy.mvps.org
DbaMgr2k ver 0.7.0 - DbaMgr ver 0.53.0
(my vb6+sql-dmo little try to provide MS MSDE 1.0 and MSDE 2000 a visual
interface)
-- remove DMO to reply
|||I think that does it. Here is what my co-worker did:
During MSDE setup, use SECURITYMODE=SQL
Then use SVRNETCN.exe and enable TCP/IP as a network library. I did this on
both machines, but you may not need to do it for both (wasn't sure, but didn't
think it mattered).
Here's the connection string that was working -
Provider=SQLOLEDB;Data Source=wrctest;user id=sa;password=wrctest;Initial
Catalog=winthrop;Net=dbmssocn;Auto Translate=True;Persist Security
Info=False;Use Encryption for Data=False

How to connect to a SQL view NOT table

Normally i can use Web Matrix to connect to a SQL table. e.g.
Dim queryString As String = "SELECT [MyTable].* FROM [MyTable]"
Is is possible to connect to a SQL View. The reason is because i have generated a view using UNIONS and JOINS and some columns are generated by concatenating columns together (ie.. has alias columns).
I also need to use VB.NET to refer to these alias columns.

hy..
I suppose it's the same as that in a table.
Dim queryString As String = "SELECT * from [MyView]"
...|||

Hey,

I think you can just use 'SELECT [MyView].* FROM [MyView]' as a query.
We use views here, we query them like they were tables, so ...
If you still have problems with it, let me know and i'll look into it a little deeper

Monday, March 12, 2012

How to configure connection string for remote server?

Sorry for the newbie question, but...

I have developed a website on my local development machine. When I create the connections string VS automatically creates a path to my local hard drive inside the string.

Data Source=.\SQLEXPRESS;AttachDbFilename="C:\Documents and Settings\Mike\My Documents\Visual Studio 2005\WebSites\PostAlertz\App_Data\PADatabase.mdf";Integrated Security=True;Connect Timeout=30;User Instance=True

However, when I deploy this website to a remote server, SQLExpress tries to attach to the file using the wrong path. How do I fix this?

Your patience is appreciated...Smile

Hi

Here is a sample connection string,and you can find more onhttp://www.connectionstrings.com/:

Data Source=myServerAddress;Initial Catalog=myDataBase;User Id=myUsername;Password=myPassword;

HOW-TO: Database from VWD to Shared Host tells you how to copy database to share host.

Hope this helps.

|||If you have SQL Express instance installed on the remote server, just set the correct path for the mdf file like:

data source=.\SQLEXPRESS;Integrated Security=SSPI;AttachDBFilename=|DataDirectory|PADatabase.mdf;User Instance=true

how to configure connection pooling

Hi,
Is there a way to control the connection pools using SQL Server 2000? I
know how to alter the connection string to do
this(http://msdn.microsoft.com/library/d...ry/en-us/cpguid
e/html/cpconconnectionpoolingforsqlservernetdataprovider. asp), but I am
looking for a setting in SQL Server that allows me ultimate control over all
connections to the DB. eg: I'd like all connection pools to timeout after
5min of inactivity, set the maximum pool size, etc. I am assuming that there
is some part of the GUI that allows this, or maybe even some (ugh) registry
editing! ;-)

Thanks,
LanceThis is all controlled on the client side. I'm not aware of anything that
can be configured within SQL Server itself that would affect this in the way
you want.

--
HTH

Jasper Smith (SQL Server MVP)

I support PASS - the definitive, global
community for SQL Server professionals -
http://www.sqlpass.org

"Lance" <lancestuff27[nospam]@.hotmail.com> wrote in message
news:104bba8p9eo5333@.corp.supernews.com...
> Hi,
> Is there a way to control the connection pools using SQL Server 2000? I
> know how to alter the connection string to do
this(http://msdn.microsoft.com/library/d...ry/en-us/cpguid
> e/html/cpconconnectionpoolingforsqlservernetdataprovider. asp), but I am
> looking for a setting in SQL Server that allows me ultimate control over
all
> connections to the DB. eg: I'd like all connection pools to timeout after
> 5min of inactivity, set the maximum pool size, etc. I am assuming that
there
> is some part of the GUI that allows this, or maybe even some (ugh)
registry
> editing! ;-)
> Thanks,
> Lance

how to configure connection pooling

Hi,
Is there a way to control the connection pools using SQL Server 2000? I
know how to alter the connection string to do
this(http://msdn.microsoft.com/library/d...ry/en-us/cpguid
e/html/ cpconconnectionpoolingforsqlservernetdat
aprovider.asp), but I am
looking for a setting in SQL Server that allows me ultimate control over all
connections to the DB. eg: I'd like all connection pools to timeout after
5min of inactivity, set the maximum pool size, etc. I am assuming that there
is some part of the GUI that allows this, or maybe even some (ugh) registry
editing! ;-)
Thanks,
LanceThis is all controlled on the client side. I'm not aware of anything that
can be configured within SQL Server itself that would affect this in the way
you want.
HTH
Jasper Smith (SQL Server MVP)
I support PASS - the definitive, global
community for SQL Server professionals -
http://www.sqlpass.org
"Lance" <lancestuff27[nospam]@.hotmail.com> wrote in message
news:104bba8p9eo5333@.corp.supernews.com...
> Hi,
> Is there a way to control the connection pools using SQL Server 2000? I
> know how to alter the connection string to do
>
this(http://msdn.microsoft.com/library/d...ry/en-us/cpguid
> e/html/ cpconconnectionpoolingforsqlservernetdat
aprovider.asp), but I am
> looking for a setting in SQL Server that allows me ultimate control over
all
> connections to the DB. eg: I'd like all connection pools to timeout after
> 5min of inactivity, set the maximum pool size, etc. I am assuming that
there
> is some part of the GUI that allows this, or maybe even some (ugh)
registry
> editing! ;-)
> Thanks,
> Lance
>

Friday, March 9, 2012

how to concatenate multiple rows into one

I want to Concatenate multiple rows into one string to show on the
report. Can someone help plz.Adnan,
U need to use Cursors...in the stored procedure.
Thats the only solution i know.....
"Adnan" wrote:
> I want to Concatenate multiple rows into one string to show on the
> report. Can someone help plz.
>

How to compose the connection String of a SSIS package that execute another package?

Dear All,

I now have two SSIS package, "TESTING" and "LOADING". The "TESTING" package have an execute package task that call the "LOADING" package. When I want to execute the TESTING package, how can I setup the connection string so that I can edit the password of the database connected by the "LOADING" package?

Regards,

Strike

You should use configurations to achieve this. There's plenty of material out there about SSIS configurations if you google it.

-Jamie

|||How about if I want to create a command file so that I can input the password externally in the command prompt?|||

You can pass values into a package via the command-line using the /SET option of dtexec.

If the package that you call is a parent package then you can pass the value through to the child using a parent package configuration.

-Jamie

|||

Hi Jamie,

I would like to know how can I setup the command line in the SQL Server Job Agent for executing the two packages mentioned above? I have already configured the parent variable for the child package. However, I have encountered some errors when executing the job, and I don't know how to set up the path for the child package in the command line as both of the packages are loaded from the SQL server, not from the file system.

Thank you very much!!

Strike Wong

|||For help building command lines, try DTExecUI.exe. Set the options you want, and then look at the Command Line page. You can copy the command from there. Obviously you can execute the package from there as well to check the validity of the command you have built.|||

Is this included in the MS SQL Server 2005?
I can't found it in the directories

|||

Yes -

How to: Run a Package Using the DTExecUI Utility
(http://msdn2.microsoft.com/en-us/library/d827d7b2-2eb6-4f4a-a775-ab00c444c0dd.aspx)

It should be available from a command prompt or the Run dialog, as the Bin folder were it lives should be on your PATH, set as part of the SQL 2005 install.

Wednesday, March 7, 2012

How to compare two strings

How can I compare two text strings?
DIFFERENCE is not very precise. Is there another string function in T-SQL?
What do you mean by compare? Presumably not =.
Have a look at patindex and soundex.
"Dave" wrote:

> How can I compare two text strings?
> DIFFERENCE is not very precise. Is there another string function in T-SQL?
>
>

How to compare two strings

How can I compare two text strings?
DIFFERENCE is not very precise. Is there another string function in T-SQL?What do you mean by compare? Presumably not =.
Have a look at patindex and soundex.
"Dave" wrote:

> How can I compare two text strings?
> DIFFERENCE is not very precise. Is there another string function in T-SQL
?
>
>

How to compare two strings

How can I compare two text strings?
DIFFERENCE is not very precise. Is there another string function in T-SQL?What do you mean by compare? Presumably not =.
Have a look at patindex and soundex.
"Dave" wrote:
> How can I compare two text strings?
> DIFFERENCE is not very precise. Is there another string function in T-SQL?
>
>

Friday, February 24, 2012

How to Combine Multiple Rows Data into single Record or String based on a common field.

Hellow Folks.

Here is the Original Data in my single SQL 2005 Table:

Department: Sells:

1 Meat

1 Rice

1 Orange

2 Orange

2 Apple

3 Pears

The Data I would like read separated by Semi-colon:

Department: Sells:

1 Meat;Rice;Orange

2 Orange;Apple

3 Pears

I would like to read my data via SP or VStudio 2005 Page . Any help will be appreciated. Thanks..

Hi,

you can use the following Function in SQL server:

USE NORTHWIND
GO

CREATE FUNCTION ProductList (@.CategoryIDINT)
RETURNSVARCHAR(1000)
AS
BEGIN
DECLARE @.ProductsVARCHAR(1000)

SELECT@.Products =COALESCE(@.Products +', ','') + ProductName
FROM Products
WHERE CategoryID = @.CategoryID
ORDER BY ProductNameASC

RETURN @.Products
END
GO

SELECTDISTINCT CategoryID, dbo.ProductList (CategoryID)AS ProductList
FROM Products
GO

 
and this is based on your table: 
 
USE NORTHWINDCS
GO

CREATE FUNCTION ProductList (@.CategoryIDINT)
RETURNSVARCHAR(1000)
AS
BEGIN
DECLARE @.ProductsVARCHAR(1000)

SELECT@.Products =COALESCE(@.Products +', ','') + sells
FROM table1
WHERE Department = @.CategoryID
ORDER BY sellsASC

RETURN @.Products
END
GO

SELECTDISTINCT Department, dbo.ProductList (department)AS ProductList
FROM table1
GO

thanks

|||

SharpGuy, your solution works. Thanks and have a great Thanksgiving...