Showing posts with label code. Show all posts
Showing posts with label code. Show all posts

Friday, March 30, 2012

How to makw Conditional Statements in SELECT

I'm trying to code a storedprocedure which looks for a product in a table. It takes one Paramater. Value may be 1 or 2 or 3.

CREATE PROCEDURE [dbo].[procSearchProduct]

@.PromoPrm int

AS

BEGIN

SELECT *

FROM tblProduct AS P

WHERE ?

END

END

--

if @.PromoPrm = 1 then where statement will be like this one: WHERE P.Promo = 'True'

else if @.PromoPrm = 2 then where statement will be like this one: WHERE P.Promo = 'False'

else if @.PromoPrm = 3 then where statement will be like this one: WHERE P.Promo = 'False' OR P.Promo = 'True'

Hope i am clear on my problem. Let me know if you don't understand it. Waiting for a solution.

Happy Coding...

One method is to us if/else:

IF @.PromoPrm = 1

select * from tblProduct p

where p.promo = 'True'

else if @.promo = 2

select * from tblProduct p

where p.promo = 'False'

else if @.promoPrm = 3

select * from tblProduct p

where p.promo = 'False'

or p.promo = 'True'

If your PROMO column can only have the values of TRUE or FALSE you can leave out the WHERE condition on the IF @.promoPrm = 3 portion.

The next comment is that you should avoid using the SELECT * syntax in stored procedures. Give a look to this post concerning this issue:

http://forums.microsoft.com/MSDN/ShowPost.aspx?PostID=1108698&SiteID=1

Use of the 'SELECT *' syntax in stored procedures can leave behind "land mines" that go off at a later date and time when changes are made to the columns of the source table.

Another approach to the problem is something like:

select * from tblProduct p

where @.promoPrm = 1

and p.promo = 'TRUE'

or @.promoPrm = 2

and p.promo = 'FALSE'

or @.promoPrm = 3

and ( p.promo = 'TRUE' OR

p.promo = 'FALSE'

)

|||

Here's a version using dynamic SQL.

declare @.sql varchar(max);

declare @.where varchar(max);

set @.where = case @.promoprm

when 1 Then'WHERE Promo = ''True'''

when 2 Then 'WHERE Promo = ''False'''

when 3 Then 'WHERE Promo = ''True'' OR Promo = ''False'''

end;

set @.sql = 'SELECT * FROM tblProducts ' + @.where;

exec(@.sql);

Jamie

|||

Yeap it is a solution but i gave little part of the code. Trying to make a seach form in asp.net . And u know this kind of page sends many parameters to sql. I can't make all combination in sql. I mean what about if this stored procedure takes 5 Parameters like @.PromoPrm . Point this problem is i have to write a code which makes decision on WHERE part. May be like this one :

SELECT *

FROM tblProduct AS P

WHERE case when P.Promo = 1 then P.Promo = 'True' else P.Promo = 'False'

This query is wrong but i hope u got the point?

|||

Beware using any dynamic SQL, especially in a web environment. You don't want to leave yourself open to code injection.

Jamie

|||

To simplyfy this,

Code Snippet

select * from tblProduct p

where @.promoPrm & 1 = 1 and p.promo = 'TRUE'

or @.promoPrm & 2 = 2 and p.promo = 'FALSE'

|||

Give a look to the "dynamic search" article by MVP Erland Sommarskog:

http://www.sommarskog.se/dyn-search.html

Also, it will help if you finish describing your problem rather giving information in portions.

|||

JHunter i think your solution is good but sql injection is important in this stiuation. If i use your code sample than i need to check the paramateres in my code but it is very very difficult because it takes it from query string. Is there any solution to this problem?

Kent thanks for post. Also thanks for link that u gave.

|||

ManiD wrote:

To simplyfy this,

Code Snippet

select * from tblProduct p

where @.promoPrm & 1 = 1 and p.promo = 'TRUE'

or @.promoPrm & 2 = 2 and p.promo = 'FALSE'

ManiD i think your solution is the best. What about the performance. Because i will use this on 5 different paramaters.

|||

If you are only passing an integer to the stored proc, leaving the proc to decide how the where clause is constructed, you'll be okay. Problems come when you start passing strings that contain SQL as parameters .

The simplest check you can do is make sure the parameter contains only "expected" values.

Erlands (as previously posted) page provides some of the best content relating to dynamic SQL.

Jamie

|||How many possible values are there for the PROMO column? If it is but a handful this will probably scan and not seek because of the cardinalities.|||

Ok here is the problem again: I'm trying to make a search form. Form runs a stored procedure call it procSearchProduct. This stored procedure takes 6 parameters.

@.CityPrm varchar(50), --takes the name of city

@.HasPromotionPrm int, -- takes onl 3 value. 1,2 or 3. 1=True , 2=Fale , 3=True OR False

@.HasDescriptionPrm, int,-- takes onl 3 value. 1,2 or 3. 1=True , 2=Fale , 3=True OR False

@.HasImagePrm int,-- takes onl 3 value. 1,2 or 3. 1=True , 2=Fale , 3=True OR False

@.StateofShopPrm varchar(50), -- takes the name of the state

@.ShopClosedOpenedPrm int -- takes onl 3 value. 1,2 or 3. 1=True , 2=Fale , 3=True OR False

what i am trying to do is select values from table. You know the rest. Sory i couldn't paste the whole code because i wrting it on dofferent language so it will not help.

|||

I'd avoid passing CityPrm and StateofShopPrm as varchars. Instead, create lookup tables for the city and state with a numeric primary key.

Populate any dropdown menus from these lookups, but capture the primary key (ID) of the selected value - not the name. This numeric ID can be passed to the stored procedure.

If you've not normalised your main table, you can use the primary key to lookup the textual value for the lookup.

Jamie

|||Also, on these VARCHAR fields, are you doing = searches or LIKE searches?|||Another point , user selects the option "WHOLE WORD" or "SOME OF THEM" in drop down for search condition on City but it will mess the problem much more. Assume that i make = search. And I cant take the State in to a dropdownlist. May be a solution but not for me.

How to make Zip Code 5 characters and not 4 in Excell

Is there a way to get 5 character zip code in excell without making it
into a TEXT?
ex:
zip code: 1024
I want it : 01024
BUT (It's a big but) I dont want it to be text - I need it to stay a
numercial # so that excell can read it as a zip code.
Is this possible?When you format the cells, look under Special - there is a ZipCode
format
Sorcerdon wrote:
> Is there a way to get 5 character zip code in excell without making it
> into a TEXT?
> ex:
> zip code: 1024
> I want it : 01024
> BUT (It's a big but) I dont want it to be text - I need it to stay a
> numercial # so that excell can read it as a zip code.
> Is this possible?|||Where is this located?
In the formating all I have is:
Default
Number
Date
Time
Percentage
Currency

Monday, March 26, 2012

How to make MDAC 2.8 behave like 2.7 regarding 'Object was open' e

On SQL Server 2K, recently put on SP4. Some VB code began to break with a
-2147217915 'Object was open' error. The code is opening a new
ADODB.Recordset, and that recordset is already open. This is clearly a bug
in the VB code, but the bug was tolerated under MDAC 2.7. The bug is
present in many code segments so it will take some time to fix it.
My DBA is hounding me to fix the code, he wants to get SP4 on to fix a
memory problem.
Is there a way to make MDAC 2.8 work like MDAC 2.7. That is, can MDAC 2.8
be configured to tolerate opening an open recordset?
BBHi
I don't think there is an option to do this. There are several versions of
2.8 you may want to check that is consistent (with the component checker) an
d
if you are on the SP1.
http://msdn.microsoft.com/data/mdac...ds/default.aspx
John
"bearcreek" wrote:

> On SQL Server 2K, recently put on SP4. Some VB code began to break with a
> -2147217915 'Object was open' error. The code is opening a new
> ADODB.Recordset, and that recordset is already open. This is clearly a b
ug
> in the VB code, but the bug was tolerated under MDAC 2.7. The bug is
> present in many code segments so it will take some time to fix it.
> My DBA is hounding me to fix the code, he wants to get SP4 on to fix a
> memory problem.
> Is there a way to make MDAC 2.8 work like MDAC 2.7. That is, can MDAC 2.8
> be configured to tolerate opening an open recordset?
> --
> BBsql

How to make MDAC 2.8 behave like 2.7 regarding 'Object was open' e

On SQL Server 2K, recently put on SP4. Some VB code began to break with a
-2147217915 'Object was open' error. The code is opening a new
ADODB.Recordset, and that recordset is already open. This is clearly a bug
in the VB code, but the bug was tolerated under MDAC 2.7. The bug is
present in many code segments so it will take some time to fix it.
My DBA is hounding me to fix the code, he wants to get SP4 on to fix a
memory problem.
Is there a way to make MDAC 2.8 work like MDAC 2.7. That is, can MDAC 2.8
be configured to tolerate opening an open recordset?
--
BBHi
I don't think there is an option to do this. There are several versions of
2.8 you may want to check that is consistent (with the component checker) and
if you are on the SP1.
http://msdn.microsoft.com/data/mdac/downloads/default.aspx
John
"bearcreek" wrote:
> On SQL Server 2K, recently put on SP4. Some VB code began to break with a
> -2147217915 'Object was open' error. The code is opening a new
> ADODB.Recordset, and that recordset is already open. This is clearly a bug
> in the VB code, but the bug was tolerated under MDAC 2.7. The bug is
> present in many code segments so it will take some time to fix it.
> My DBA is hounding me to fix the code, he wants to get SP4 on to fix a
> memory problem.
> Is there a way to make MDAC 2.8 work like MDAC 2.7. That is, can MDAC 2.8
> be configured to tolerate opening an open recordset?
> --
> BB

Wednesday, March 21, 2012

How to loop thru' Fields in Report

I'm using Reporting Server 2005. I have published my report as Web service. Now I'm accessing the Web Service (Report) from my C# code. Here I would like to loop all the fields available in the report. How to do this?

Thanks in Advance.

Regards,

vnisor.

Have you considered using one of the data renderers (such as XML)? When you render it will only return the data in the report and not any formatting. It should make it much easier to extract the data you are interested in programmatically.

Monday, March 19, 2012

How to log uses of SELECT

Hello,
Here's my scenario:
-I have a vendor application running sql2k.
-I do not control the code and the vendor isn't currently interested in implementing what I want.
-I keep medical data in this app.
-Patients, by law, are entitled to know who's seen (not just changed or entered) their data.
-So I need to be able to keep a log of which users have seen which patient files, but I don't control the app code (and wish to god there were such a thing as a select trigger).
If Joe comes to me soon, and says "I need to know who's seen my data." I'd like to do something like:
select username
from audit_selects
where patient_id = XXX
and get a list of people who've seen this.
Can I do this without necessarily being able to write a logging procedure into the app?
Thanks,
JohnThere is no trigger on SELECT, and Profiler is probably not valuable enough
for the cost of it running constantly, but you should try it out. You might
investigate some of the auditing tools listed in http://www.aspfaq.com/2496
--
Aaron Bertrand
SQL Server MVP
http://www.aspfaq.com/
"John" <anonymous@.discussions.microsoft.com> wrote in message
news:3F88A456-3DFF-4362-A1F8-E130E1A546E4@.microsoft.com...
> Hello,
> Here's my scenario:
> -I have a vendor application running sql2k.
> -I do not control the code and the vendor isn't currently interested in
implementing what I want.
> -I keep medical data in this app.
> -Patients, by law, are entitled to know who's seen (not just changed or
entered) their data.
> -So I need to be able to keep a log of which users have seen which patient
files, but I don't control the app code (and wish to god there were such a
thing as a select trigger).
> If Joe comes to me soon, and says "I need to know who's seen my data." I'd
like to do something like:
> select username
> from audit_selects
> where patient_id = XXX
> and get a list of people who've seen this.
> Can I do this without necessarily being able to write a logging procedure
into the app?
> Thanks,
> John
>|||> If Joe comes to me soon, and says "I need to know who's seen my data." I'd
like to do something like:
> select username
> from audit_selects
> where patient_id = XXX
> and get a list of people who've seen this.
Yikes, so if username does SELECT * FROM patients they're going to get a row
in your audit table for every row in your data?
--
Aaron Bertrand
SQL Server MVP
http://www.aspfaq.com/|||Here's my scenario:
-I have a vendor application running sql2k.
-I do not control the code and the vendor isn't currently interested in
implementing what I want.
-I keep medical data in this app.
-Patients, by law, are entitled to know who's seen (not just changed or
entered) their data.
-So I need to be able to keep a log of which users have seen which patient
files, but I don't control the app code (and wish to god there were such a
thing as a select trigger).
If Joe comes to me soon, and says "I need to know who's seen my data." I'd
like to do something like:
select username
from audit_selects
where patient_id = XXX
and get a list of people who've seen this.
Can I do this without necessarily being able to write a logging procedure
into the app?
Thanks,
John
--
It would be difficult because selects are not logged. You can turn on
profiler trace and capture all selects on specific tables. But reviewing
this output is not as straightforward as filtering by a certain patient_id.
Hope this helps,
--
Eric Cárdenas
SQL Server support|||Take a look at Lumigent Entegra (www.lumigent.com), which
is an audit tool for SQL Server. I'm not affiliated with
them, but I'm currently evaluating the product. It does
audit queries.
Linchi
>--Original Message--
>Hello,
>Here's my scenario:
>-I have a vendor application running sql2k.
>-I do not control the code and the vendor isn't currently
interested in implementing what I want.
>-I keep medical data in this app.
>-Patients, by law, are entitled to know who's seen (not
just changed or entered) their data.
>-So I need to be able to keep a log of which users have
seen which patient files, but I don't control the app code
(and wish to god there were such a thing as a select
trigger).
>If Joe comes to me soon, and says "I need to know who's
seen my data." I'd like to do something like:
>select username
>from audit_selects
>where patient_id = XXX
>and get a list of people who've seen this.
>Can I do this without necessarily being able to write a
logging procedure into the app?
>Thanks,
>John
>.
>

How to lock only one row

Hello everyone,
I need to:
1. Get data from a table row
2. Lock a row for delete
3. Delete it
The problem is that the code that I use locks the whole table, not just one
row. Does anyone know how to limit the lock to one row for one table?
Thank you,
Helen
P.S.That is what I do:
EXEC SQL
DECLARE ACURS CURSOR FOR
SELECT
"MDC7_PROG_ID",
"MDC7_REV_DATE"
FROM
"MDC7"
WHERE
"MDC7_PROG_ID" = :FMDC7-PROG-ID
END-EXEC
EXEC SQL
OPEN ACURS
END-EXEC
IF SQLCODE <> 0
GO TO END-OF-PROGRAM.
EXEC SQL
FETCH ACURS
INTO
:FMDC7-PROG-ID
, :FMDC7-REV-DATE
END-EXEC
EXEC SQL
DELETE MDC7
WHERE CURRENT OF ACURS
END-EXEC.
IF SQLCODE <> 0
EXEC SQL
CLOSE ACURS
END-EXEC.Helen Stein wrote:
> Hello everyone,
> I need to:
> 1. Get data from a table row
> 2. Lock a row for delete
> 3. Delete it
> The problem is that the code that I use locks the whole table, not
> just one row. Does anyone know how to limit the lock to one row for
> one table?
>
Do you need to be using cursors for this? If not, you can just issue the
delete directly. Unless there are no indexes to help SQL Server locate
the row, you should not see any table locking for the operation.
For example:
Declare @.ID INT
Declare @.OtherKey INT
Set @.OtherKey = 1000
Select @.ID = MyID
From TableA
Where OtherKey = @.OtherKey
If @.ID IS NOT NULL
Delete From TableB Where ID = @.ID
David Gugick
Imceda Software
www.imceda.com|||Why do you need to use a cursor to do a DELETE like this? You are deleting
all rows that are in the current cursor. Wouldn't it be much easier and
faster to write
DELETE MDC7
WHERE MDC7_PROG_ID = <what ever value FMDC7-PROG-ID represents>
How many rows of how many is this operation deleting?
GertD@.SQLDev.Net
Please reply only to the newsgroups.
This posting is provided "AS IS" with no warranties, and confers no rights.
You assume all risk for your use.
Copyright SQLDev.Net 1991-2005 All rights reserved.
"Helen Stein" <hstein@.nojunklarimor.net> wrote in message
news:e4aLcRQFFHA.1564@.TK2MSFTNGP09.phx.gbl...
> Hello everyone,
> I need to:
> 1. Get data from a table row
> 2. Lock a row for delete
> 3. Delete it
> The problem is that the code that I use locks the whole table, not just
> one
> row. Does anyone know how to limit the lock to one row for one table?
> Thank you,
> Helen
> P.S.That is what I do:
>
> EXEC SQL
> DECLARE ACURS CURSOR FOR
> SELECT
> "MDC7_PROG_ID",
> "MDC7_REV_DATE"
> FROM
> "MDC7"
> WHERE
> "MDC7_PROG_ID" = :FMDC7-PROG-ID
> END-EXEC
>
> EXEC SQL
> OPEN ACURS
> END-EXEC
> IF SQLCODE <> 0
> GO TO END-OF-PROGRAM.
>
> EXEC SQL
> FETCH ACURS
> INTO
> :FMDC7-PROG-ID
> , :FMDC7-REV-DATE
> END-EXEC
>
> EXEC SQL
> DELETE MDC7
> WHERE CURRENT OF ACURS
> END-EXEC.
>
> IF SQLCODE <> 0
> EXEC SQL
> CLOSE ACURS
> END-EXEC.
>|||The problem is your use of a cursor. It looks like you want something
like this (in TSQL):
SET @.mdc7_rev_date =
(SELECT mdc7_rev_date
FROM MDC7
WHERE mdc7_prog_id = @.mdc7_prog_id)
DELETE FROM MDC7
WHERE mdc7_prog_id = @.mdc7_prog_id
AND mdc7_rev_date = @.mdc7_rev_date
David Portas
SQL Server MVP
--|||The reason why I am using a cursor is because I need to make sure that in
between my select and delete the other user cannot access the row I just
read. Is there any other way of doing this?
Thanks for everybodys reponses.
"David Gugick" <davidg-nospam@.imceda.com> wrote in message
news:OQdynVQFFHA.2676@.TK2MSFTNGP12.phx.gbl...
> Helen Stein wrote:
> Do you need to be using cursors for this? If not, you can just issue the
> delete directly. Unless there are no indexes to help SQL Server locate
> the row, you should not see any table locking for the operation.
> For example:
> Declare @.ID INT
> Declare @.OtherKey INT
> Set @.OtherKey = 1000
> Select @.ID = MyID
> From TableA
> Where OtherKey = @.OtherKey
> If @.ID IS NOT NULL
> Delete From TableB Where ID = @.ID
>
> --
> David Gugick
> Imceda Software
> www.imceda.com
>|||Isn't handled through transaction isolation?
GertD@.SQLDev.Net
Please reply only to the newsgroups.
This posting is provided "AS IS" with no warranties, and confers no rights.
You assume all risk for your use.
Copyright SQLDev.Net 1991-2005 All rights reserved.
"Helen Stein" <hstein@.nojunklarimor.net> wrote in message
news:Oub2T3QFFHA.3504@.TK2MSFTNGP12.phx.gbl...
> The reason why I am using a cursor is because I need to make sure that in
> between my select and delete the other user cannot access the row I just
> read. Is there any other way of doing this?
>
> Thanks for everybody's reponses.
> "David Gugick" <davidg-nospam@.imceda.com> wrote in message
> news:OQdynVQFFHA.2676@.TK2MSFTNGP12.phx.gbl...
>

Wednesday, March 7, 2012

How to launch a SSIS package from VB or whatever

Dear all,

I am looking for any snippet of code where you can launch a SSIS package by DMO or VB 6.0. I read that it was posible to call dts.runtime assembly from VB 6.0 but at first it might be converted or something like that.

Issue comes from the moment that we’ve got an ASP 3.0 scheduler for hundreds of dts and now we have to migrate them to sql25k.

Thanks in advance for any info regarding this,

Hey there Enric,

I'm no expert in SSIS.. but I AM reading the Wrox book "Professional SQL Server 2005 Integration Services" which has a chapter on using SSIS with external applications.

If you get the book just look at chapter 17 and you'll find the info you need.

OR you could go to Wrox.com and download the sample code for chapter 17. It contains sample VB to call an SSIS package.

Basically you have to use the Microsoft.SqlServer.Dts.DtsClient namespace...

You import that namespace and use it to connect to the package...

Imports Microsoft.SqlServer.Dts.DtsClient

Then use an DtsCommand to execute the the package.

I don't want to post Wrox's sample code here (don't want no trouble) but if you search in books online for DtsCommand you should find stuff....

Its a bit silly calling an SSIS package with a DtsCommand isn't it?

Very confusing..

PJ

|||

The reason for the false naming is that the product was well into the development cycle when the decision was made to change the name from DTS to SSIS. That decision was actually made around Q3 2004, almost 4 years after development started.

At that late stage in the game it was considered too much of a job to go through and change all the code from DTS* to SSIS*. Its a shame but can't be helped.

Do as all techies do...blame the marketing department :)

-Jamie

|||

On Books online at

http://msdn2.microsoft.com/en-us/library/aa337077.aspx

there's an extensive coverage on how to run SSIS packages remotely.

You may need to be a bit seasoned developer to go through it and run it though, as the documentation is not a step-by-step instruction.

Rgds,

How to launch a SSIS package from VB or whatever

Dear all,

I am looking for any snippet of code where you can launch a SSIS package by DMO or VB 6.0. I read that it was posible to call dts.runtime assembly from VB 6.0 but at first it might be converted or something like that.

Issue comes from the moment that we’ve got an ASP 3.0 scheduler for hundreds of dts and now we have to migrate them to sql25k.

Thanks in advance for any info regarding this,

Hey there Enric,

I'm no expert in SSIS.. but I AM reading the Wrox book "Professional SQL Server 2005 Integration Services" which has a chapter on using SSIS with external applications.

If you get the book just look at chapter 17 and you'll find the info you need.

OR you could go to Wrox.com and download the sample code for chapter 17. It contains sample VB to call an SSIS package.

Basically you have to use the Microsoft.SqlServer.Dts.DtsClient namespace...

You import that namespace and use it to connect to the package...

Imports Microsoft.SqlServer.Dts.DtsClient

Then use an DtsCommand to execute the the package.

I don't want to post Wrox's sample code here (don't want no trouble) but if you search in books online for DtsCommand you should find stuff....

Its a bit silly calling an SSIS package with a DtsCommand isn't it?

Very confusing..

PJ

|||

The reason for the false naming is that the product was well into the development cycle when the decision was made to change the name from DTS to SSIS. That decision was actually made around Q3 2004, almost 4 years after development started.

At that late stage in the game it was considered too much of a job to go through and change all the code from DTS* to SSIS*. Its a shame but can't be helped.

Do as all techies do...blame the marketing department :)

-Jamie

|||

On Books online at

http://msdn2.microsoft.com/en-us/library/aa337077.aspx

there's an extensive coverage on how to run SSIS packages remotely.

You may need to be a bit seasoned developer to go through it and run it though, as the documentation is not a step-by-step instruction.

Rgds,

Friday, February 24, 2012

How to know the code in Query Analyser

My Program wants to know what is being edited in Query Analyser currently.
How can I do that?

Thanks.

I would not consider it nice for an application to snoop inside of another unless there is a system to addin that functionality.

Why do you want to know what is beeing edited?

One approach if you want to get the edited text is to use the Process class to launch query analyzer editing a temporary file. You will then add a FileSystemWatcher class to check whenever the user is saving the changes to the temporaryfile. You can then pick up any changes and in that way get what is in query analyzer. But only when the user chooses to save.

When the process closes you can remove the watching of the file and the file.

|||

Thank you 4 your answer,

I just want My program to prompt the SQL,tables and so on in MS Query Analyser when the user press the Space Key or Dot Key(".") in the SQL editor.

What is your opinion?

|||

You will have to do it in your own query tool. You can have a look at Query Commander and its source code, it is a query tool with intellisense. It is written in C#.

|||

Thank you 4 your suggestion,

There is an interesting tool called promptsql, http://www.promptsql.com/

how does it work? do you have any idea?

|||

I would not consider it nice for an application to snoop inside of another unless there is a system to addin that functionality.

For query analyzer I am not familiar with any addin functionality so I would suspect that promptsql installs hooks and finds out if the active window belongs to certain application like query analyzer when certain keyboard sequences has been entered.