Showing posts with label bit. Show all posts
Showing posts with label bit. Show all posts

Wednesday, March 28, 2012

Query with multiple arguments?

Hi, I'm a bit stumped as to how to do this.
I have a string[] with a list of users, and I want to query my database to select only the users in this array and bind the datasource to a GridView, but I don't know how to write an SQL query to search for multiple results from the same field.

E.g. Say I have two results in my string[], fred and bob.
How can I select data from the database for just those two users - "SELECT * FROM tblUsers WHERE UserName='bob' AND ??";

IF this is possible, I also need to bind it to a gridview. I tried the following, but it didn't work as I needed it to:

for(int a = 0; a < userArray.Length; a++)
{
conn.Open();
SqlCommand command = new SqlCommand("SELECT * FROM tblUsers WHERE UserName='" + userArray[a] + "'", conn);
SqlDataReader reader = command.ExecuteReader();
grid.DataSource = reader;
grid.DataBind();
conn.Close()
}

That 'worked', but as I'm sure you can see, the data that was bound to the gridview was only the last result found, not the whole result set.

Any help is greatly appreciated.

schuminator:

for(int a = 0; a < userArray.Length; a++)
{
conn.Open();
SqlCommand command = new SqlCommand("SELECT * FROM tblUsers WHERE UserName='" + userArray[a] + "'", conn);
SqlDataReader reader = command.ExecuteReader();
grid.DataSource = reader;
grid.DataBind();
conn.Close()
}

try out as below

 String strUsers = String.Empty;for (int a = 0; a < userArray.Length; a++) strUsers = strUsers + @."'" + userArray[a] + @."',"; strUsers = strUsers.Substring(0, strUsers.Length - 1); conn.Open(); SqlCommand command =new SqlCommand("SELECT * FROM tblUsers WHERE UserName in (" + strUsers +")", conn); SqlDataReader reader = command.ExecuteReader(); grid.DataSource = reader; grid.DataBind(); conn.Close();

Good Luck./.

|||

Sorry, I tried to delete the thread but it was too late.
I got it sorted...it was so simple, I feel like an idiot!

"SELECT * FROM tblUsers WHERE UserName='fred' OR UserName='bob'" etc

I also wrote a little for loop to add another OR... to the end of the string when necessary.

|||

you can use above method also...

so instead of UserName='fred' OR UserName='bob'"......

if will formulate query using IN keywork as..

UserName in ('fred','bob')

|||

ahh ok awesome, thanks :)

Monday, March 26, 2012

query with a column per row of a linked table - dynamic sql

I've thought about this a bit more and relised that it can be done using
dynamic SQL, but I would like to avoid this if possible. What are the other
options?
"WCL" <WCL@.nospam.nospam> wrote in message
news:usRAcZGIGHA.1312@.TK2MSFTNGP09.phx.gbl...
> Is it possible to have query result to have a column per row of a table?
>
> e.g.
> Employees table
> ID (identity)
> FirstName
> Ref
>
> containing
> ID, FirstName
> 1 Tom
> 2 Dick
> 3 Harry
>
>
> Project table
> ID (identity)
> Name
>
> containing
> ID, Name
> 1 Client A
> 2 Client B
> 3 Client C
>
> Timesheets
> ID (identity)
> Employees_ID
> Project_ID
> Hours
>
> containing 6 rows
> ID
> Employee_ID
> Project_ID
> Hours
> 1
> 1
> 1
> 5
> 2
> 1
> 2
> 15
> 3
> 2
> 1
> 2
> 4
> 2
> 2
> 4
> 5
> 3
> 1
> 8
> 6
> 3
> 2
> 8
>
> NB - No records for client C
>
> I can get three columns (name, client, hours) with nine rows no problem,
> but how do I get 3 rows with a column per Client, like this:?
>
> Name
> Client A
> Client B
> Client C
> Tom
> 5
> 15
> 0 or NULL
> Dick
> 2
> 4
> 0 or NULL
> Harry
> 8
> 8
> 0 or NULL
>
>
>No, it is not really useful from a SQL standpoint to do this, so it is not a
part of SQL (columns should contain like things, not multiple different
things.)
I think you can do it using several UNION ALLs:
SELECT client, cast(client as varchar(30)), 1 as sorting
FROM table
UNION ALL
SELECT client,cast(value1 as varchar(30)), 2 as sorting
FROM table
order by client, sorting --maybe something different than key
I would suggest using the client tool to format data like this.
----
Louis Davidson - http://spaces.msn.com/members/drsql/
SQL Server MVP
"Arguments are to be avoided: they are always vulgar and often convincing."
(Oscar Wilde)
"WCL" <WCL@.nospam.nospam> wrote in message
news:u4q6ipGIGHA.3176@.TK2MSFTNGP12.phx.gbl...
> I've thought about this a bit more and relised that it can be done using
> dynamic SQL, but I would like to avoid this if possible. What are the
> other options?
> "WCL" <WCL@.nospam.nospam> wrote in message
> news:usRAcZGIGHA.1312@.TK2MSFTNGP09.phx.gbl...
>

Tuesday, March 20, 2012

Query to represent denormalization of data

Hi All,

Bit of a funny one. I have two tables, lets call them PetOwner and Pets

PetOwner

OwnerID OwnerName
1 Tim
2 Paul
3 Andy

Pets
OwnerID PetName
1 Tricia
1 Louise
2 Bill
2 Ben
2 David
2 Topsy
3 Flopsy
3 Mopsy

And I need to write a query that returns a resultset like this

OwnerID OwnerName PetName1 PetName2 PetName3 PetName4 PetName5
1 Tim Tricia Louise NULL NULL NULL
2 Paul Bill Ben David Topsy Flopsy
3 Andy Flopsy Mopsy NULL NULL NULL

Can anyone help?

N.B. A pet owner can only have a maximum of 5 pets, but even if no-one in the database has 5 pets, the PetName5 column must still appear (but have a value of NULL).Is this SQL 2000 or 2005?|||Doh! Sorry, SQL Server 2005.|||No it's not, it's 2000! (soon to be moved to 2005)|||No probs :)

And absolutely always a max of five? This affects the best\ easiest solution...|||Soon enouigh to accept a 2005 answer? ;)|||This should work in 2000 and 2005. There is a more efficient 2005 method so you can update it when you migrate.


SELECT po.OwnerID
, po.OwnerName
, MAX(CASE WHEN rn = 1 THEN PetName END) AS PetName1
, MAX(CASE WHEN rn = 2 THEN PetName END) AS PetName2
, MAX(CASE WHEN rn = 3 THEN PetName END) AS PetName3
, MAX(CASE WHEN rn = 4 THEN PetName END) AS PetName4
, MAX(CASE WHEN rn = 5 THEN PetName END) AS PetName5
FROM (SELECT *
, (SELECT COUNT(*) AS rec_no
FROM dbo.pets AS p2
WHERE p2.OwnerID = p.OwnerID
AND p2.PetName <= p.PetName) AS rn
FROM dbo.pets AS p) AS row_numbers
INNER JOIN
dbo.PetOwner AS po
ON po.OwnerID = row_numbers.OwnerID
GROUP BY po.OwnerID
, po.OwnerName|||That's great, I'll test it and let you know how I get on.

Cheers,|||Ok, I'm british so I shouldn't say this, but pootle flump, you rock! Thanks, it works a dream|||Ok, I'm british so I shouldn't say this, but pootle flump, you rock! Thanks, it works a dreamI'm British too so Jolly Good Show!|||Don't British people rock too? Elton? Ziggie? Mick?|||Don't British people rock too? Elton? Ziggie? Mick?... Poots?|||Elton... Rock... Rock... Elton?
Seriously!|||Elton "crocodile" rocks...

Monday, February 20, 2012

Query suddenly becomes very slow

Hi. This is a bit of an open ended question which I don't think there is a
single answer to but I woud appreciate suggestions on though. For the last
few months a stored procedure has been taking between 2-12 seconds to
complete. It runs several times a day. Suddenly today in the space of a
minute the stored procedure went from 2 seconds to complete to 35 minutes!
It was inexplicable. There was nothing else running on the machine (that I
know of) at the time. Further invocations continued to take at least 10
minutes to run. Only after bouncing the SQL Server instance did the stored
procedure go back to its normal timeframe of 2-12 seconds.
Nothing seemed out of the ordinary - it was just a regular day's processing
as had been many times in the past. How can there be such a huge decrease in
performance so rapidly? What could have caused it?
McGeeky
http://mcgeeky.blogspot.comhave you updated you table statistics?
and defrag your indexes
"McGeeky" <anon@.anon.com> wrote in message
news:OnY8TKUSGHA.5656@.TK2MSFTNGP11.phx.gbl...
> Hi. This is a bit of an open ended question which I don't think there is a
> single answer to but I woud appreciate suggestions on though. For the last
> few months a stored procedure has been taking between 2-12 seconds to
> complete. It runs several times a day. Suddenly today in the space of a
> minute the stored procedure went from 2 seconds to complete to 35 minutes!
> It was inexplicable. There was nothing else running on the machine (that I
> know of) at the time. Further invocations continued to take at least 10
> minutes to run. Only after bouncing the SQL Server instance did the stored
> procedure go back to its normal timeframe of 2-12 seconds.
> Nothing seemed out of the ordinary - it was just a regular day's
> processing
> as had been many times in the past. How can there be such a huge decrease
> in
> performance so rapidly? What could have caused it?
> --
> McGeeky
> http://mcgeeky.blogspot.com
>
>|||The database is configured to auto update stats.
Would not updating stats cause performance to drop off a cliff all of a
sudden?
McGeeky
http://mcgeeky.blogspot.com
"Jj" <willgart@.BBBhotmailAAA.com> wrote in message
news:#v41#IVSGHA.3192@.TK2MSFTNGP09.phx.gbl...
> have you updated you table statistics?
> and defrag your indexes
> "McGeeky" <anon@.anon.com> wrote in message
> news:OnY8TKUSGHA.5656@.TK2MSFTNGP11.phx.gbl...
a[vbcol=seagreen]
last[vbcol=seagreen]
minutes![vbcol=seagreen]
I[vbcol=seagreen]
stored[vbcol=seagreen]
decrease[vbcol=seagreen]
>|||auto update is not enough.
a good and planned cleansing is always better.
1 client delete and reload some tables at regular basis, if we don't update
the statistics a query against these table drop from seconds to minutes.
so updating stats and defrag indexes is really important.
regarding which way you use to defrag your indexes, the system will also
update the stats at the same time.
see the BOL and search the web for more info
"McGeeky" <anon@.anon.com> wrote in message
news:utYEAPSTGHA.5924@.TK2MSFTNGP09.phx.gbl...
> The database is configured to auto update stats.
> Would not updating stats cause performance to drop off a cliff all of a
> sudden?
> --
> McGeeky
> http://mcgeeky.blogspot.com
>
> "Jj" <willgart@.BBBhotmailAAA.com> wrote in message
> news:#v41#IVSGHA.3192@.TK2MSFTNGP09.phx.gbl...
> a
> last
> minutes!
> I
> stored
> decrease
>

Query suddenly becomes very slow

Hi. This is a bit of an open ended question which I don't think there is a
single answer to but I woud appreciate suggestions on though. For the last
few months a stored procedure has been taking between 2-12 seconds to
complete. It runs several times a day. Suddenly today in the space of a
minute the stored procedure went from 2 seconds to complete to 35 minutes!
It was inexplicable. There was nothing else running on the machine (that I
know of) at the time. Further invocations continued to take at least 10
minutes to run. Only after bouncing the SQL Server instance did the stored
procedure go back to its normal timeframe of 2-12 seconds.
Nothing seemed out of the ordinary - it was just a regular day's processing
as had been many times in the past. How can there be such a huge decrease in
performance so rapidly? What could have caused it?
--
McGeeky
http://mcgeeky.blogspot.comhave you updated you table statistics?
and defrag your indexes
"McGeeky" <anon@.anon.com> wrote in message
news:OnY8TKUSGHA.5656@.TK2MSFTNGP11.phx.gbl...
> Hi. This is a bit of an open ended question which I don't think there is a
> single answer to but I woud appreciate suggestions on though. For the last
> few months a stored procedure has been taking between 2-12 seconds to
> complete. It runs several times a day. Suddenly today in the space of a
> minute the stored procedure went from 2 seconds to complete to 35 minutes!
> It was inexplicable. There was nothing else running on the machine (that I
> know of) at the time. Further invocations continued to take at least 10
> minutes to run. Only after bouncing the SQL Server instance did the stored
> procedure go back to its normal timeframe of 2-12 seconds.
> Nothing seemed out of the ordinary - it was just a regular day's
> processing
> as had been many times in the past. How can there be such a huge decrease
> in
> performance so rapidly? What could have caused it?
> --
> McGeeky
> http://mcgeeky.blogspot.com
>
>|||The database is configured to auto update stats.
Would not updating stats cause performance to drop off a cliff all of a
sudden?
--
McGeeky
http://mcgeeky.blogspot.com
"Jéjé" <willgart@.BBBhotmailAAA.com> wrote in message
news:#v41#IVSGHA.3192@.TK2MSFTNGP09.phx.gbl...
> have you updated you table statistics?
> and defrag your indexes
> "McGeeky" <anon@.anon.com> wrote in message
> news:OnY8TKUSGHA.5656@.TK2MSFTNGP11.phx.gbl...
> > Hi. This is a bit of an open ended question which I don't think there is
a
> > single answer to but I woud appreciate suggestions on though. For the
last
> > few months a stored procedure has been taking between 2-12 seconds to
> > complete. It runs several times a day. Suddenly today in the space of a
> > minute the stored procedure went from 2 seconds to complete to 35
minutes!
> > It was inexplicable. There was nothing else running on the machine (that
I
> > know of) at the time. Further invocations continued to take at least 10
> > minutes to run. Only after bouncing the SQL Server instance did the
stored
> > procedure go back to its normal timeframe of 2-12 seconds.
> >
> > Nothing seemed out of the ordinary - it was just a regular day's
> > processing
> > as had been many times in the past. How can there be such a huge
decrease
> > in
> > performance so rapidly? What could have caused it?
> >
> > --
> > McGeeky
> > http://mcgeeky.blogspot.com
> >
> >
> >
>|||auto update is not enough.
a good and planned cleansing is always better.
1 client delete and reload some tables at regular basis, if we don't update
the statistics a query against these table drop from seconds to minutes.
so updating stats and defrag indexes is really important.
regarding which way you use to defrag your indexes, the system will also
update the stats at the same time.
see the BOL and search the web for more info
"McGeeky" <anon@.anon.com> wrote in message
news:utYEAPSTGHA.5924@.TK2MSFTNGP09.phx.gbl...
> The database is configured to auto update stats.
> Would not updating stats cause performance to drop off a cliff all of a
> sudden?
> --
> McGeeky
> http://mcgeeky.blogspot.com
>
> "Jéjé" <willgart@.BBBhotmailAAA.com> wrote in message
> news:#v41#IVSGHA.3192@.TK2MSFTNGP09.phx.gbl...
>> have you updated you table statistics?
>> and defrag your indexes
>> "McGeeky" <anon@.anon.com> wrote in message
>> news:OnY8TKUSGHA.5656@.TK2MSFTNGP11.phx.gbl...
>> > Hi. This is a bit of an open ended question which I don't think there
>> > is
> a
>> > single answer to but I woud appreciate suggestions on though. For the
> last
>> > few months a stored procedure has been taking between 2-12 seconds to
>> > complete. It runs several times a day. Suddenly today in the space of a
>> > minute the stored procedure went from 2 seconds to complete to 35
> minutes!
>> > It was inexplicable. There was nothing else running on the machine
>> > (that
> I
>> > know of) at the time. Further invocations continued to take at least 10
>> > minutes to run. Only after bouncing the SQL Server instance did the
> stored
>> > procedure go back to its normal timeframe of 2-12 seconds.
>> >
>> > Nothing seemed out of the ordinary - it was just a regular day's
>> > processing
>> > as had been many times in the past. How can there be such a huge
> decrease
>> > in
>> > performance so rapidly? What could have caused it?
>> >
>> > --
>> > McGeeky
>> > http://mcgeeky.blogspot.com
>> >
>> >
>> >
>>
>

Query suddenly becomes very slow

Hi. This is a bit of an open ended question which I don't think there is a
single answer to but I woud appreciate suggestions on though. For the last
few months a stored procedure has been taking between 2-12 seconds to
complete. It runs several times a day. Suddenly today in the space of a
minute the stored procedure went from 2 seconds to complete to 35 minutes!
It was inexplicable. There was nothing else running on the machine (that I
know of) at the time. Further invocations continued to take at least 10
minutes to run. Only after bouncing the SQL Server instance did the stored
procedure go back to its normal timeframe of 2-12 seconds.
Nothing seemed out of the ordinary - it was just a regular day's processing
as had been many times in the past. How can there be such a huge decrease in
performance so rapidly? What could have caused it?
McGeeky
http://mcgeeky.blogspot.com
have you updated you table statistics?
and defrag your indexes
"McGeeky" <anon@.anon.com> wrote in message
news:OnY8TKUSGHA.5656@.TK2MSFTNGP11.phx.gbl...
> Hi. This is a bit of an open ended question which I don't think there is a
> single answer to but I woud appreciate suggestions on though. For the last
> few months a stored procedure has been taking between 2-12 seconds to
> complete. It runs several times a day. Suddenly today in the space of a
> minute the stored procedure went from 2 seconds to complete to 35 minutes!
> It was inexplicable. There was nothing else running on the machine (that I
> know of) at the time. Further invocations continued to take at least 10
> minutes to run. Only after bouncing the SQL Server instance did the stored
> procedure go back to its normal timeframe of 2-12 seconds.
> Nothing seemed out of the ordinary - it was just a regular day's
> processing
> as had been many times in the past. How can there be such a huge decrease
> in
> performance so rapidly? What could have caused it?
> --
> McGeeky
> http://mcgeeky.blogspot.com
>
>
|||The database is configured to auto update stats.
Would not updating stats cause performance to drop off a cliff all of a
sudden?
McGeeky
http://mcgeeky.blogspot.com
"Jj" <willgart@.BBBhotmailAAA.com> wrote in message
news:#v41#IVSGHA.3192@.TK2MSFTNGP09.phx.gbl...[vbcol=seagreen]
> have you updated you table statistics?
> and defrag your indexes
> "McGeeky" <anon@.anon.com> wrote in message
> news:OnY8TKUSGHA.5656@.TK2MSFTNGP11.phx.gbl...
a[vbcol=seagreen]
last[vbcol=seagreen]
minutes![vbcol=seagreen]
I[vbcol=seagreen]
stored[vbcol=seagreen]
decrease
>
|||auto update is not enough.
a good and planned cleansing is always better.
1 client delete and reload some tables at regular basis, if we don't update
the statistics a query against these table drop from seconds to minutes.
so updating stats and defrag indexes is really important.
regarding which way you use to defrag your indexes, the system will also
update the stats at the same time.
see the BOL and search the web for more info
"McGeeky" <anon@.anon.com> wrote in message
news:utYEAPSTGHA.5924@.TK2MSFTNGP09.phx.gbl...
> The database is configured to auto update stats.
> Would not updating stats cause performance to drop off a cliff all of a
> sudden?
> --
> McGeeky
> http://mcgeeky.blogspot.com
>
> "Jj" <willgart@.BBBhotmailAAA.com> wrote in message
> news:#v41#IVSGHA.3192@.TK2MSFTNGP09.phx.gbl...
> a
> last
> minutes!
> I
> stored
> decrease
>