Showing posts with label queries. Show all posts
Showing posts with label queries. Show all posts

Tuesday, March 20, 2012

3 quick queries

I want help with a couple of SQL queries.
I have two Tables Table A and Table B.
Both tables have the same two fields Name and Hobbies.
One Name can appear beside multiple hobbies in each table.

There are three queries I wish to run.
1) Find all the Hobbie and Name combinations in Table B not in Table A
only for Names that exist in Table A
2) Find all Hobbies and Name combinations in Table A not in Table B
3) Return all data in Table B that contains a Name that exists in Table
A

Regards,
CiarnLooks like homework to me. Have you looked up some example queries and
tried to adapt them to your application? What have you tried so far?

--
David Portas
SQL Server MVP
--|||No its not homework, what I need to do is slightly different, but
thought if I got the answers to these three I'd be able to figure it
out.
I've been playing with inner and outer joins, but can't figure out what
I need to do.

Regards,
Ciarn

BTW what does MVP stand for?|||On 4 Apr 2005 08:48:30 -0700, chudson007@.hotmail.com wrote:

>I want help with a couple of SQL queries.
>I have two Tables Table A and Table B.
>Both tables have the same two fields Name and Hobbies.
>One Name can appear beside multiple hobbies in each table.
>There are three queries I wish to run.
>1) Find all the Hobbie and Name combinations in Table B not in Table A
>only for Names that exist in Table A
>2) Find all Hobbies and Name combinations in Table A not in Table B
>3) Return all data in Table B that contains a Name that exists in Table
>A
>Regards,
>Ciarn

Hi Ciarn,

I guess the real question is why you store the same data in two tables.

Try if the following work. If not, then post table structure and sample
data as described here: www.aspfaq.com/5006.

1)
SELECT b.Hobbie, b.Name
FROM TableB AS b
WHERE EXISTS (SELECT *
FROM TableA AS a
WHERE a.Name = b.Name)

2)
SELECT a.Hobbie, a.Name
FROM TableA AS a
WHERE NOT EXISTS (SELECT *
FROM TableB AS b
WHERE b.Name = a.Name
AND b.Hobbie = a.Hobbie)

3) Same as 1.

Notes:
* 1 and 3 can also be done with an inner join, but you need some way to
prevent duplicates - either DISTINCT or a derived table
* 2 can also be done with an outer join.

Best, Hugo
--

(Remove _NO_ and _SPAM_ to get my e-mail address)|||1 and 2:

SELECT A.name, A.hobby, B.name, B.hobby
FROM TableA AS A
FULL JOIN TableB AS B
ON A.name = B.name
AND A.hobby = B.hobby
WHERE A.name IS NULL
OR B.name IS NULL

3.
SELECT DISTINCT B.name, B.hobby
FROM TableA AS A
JOIN TableB AS B
ON A.name = B.name

Info on the Most Valuable Professional programme:
http://mvp.support.microsoft.com/

--
David Portas
SQL Server MVP
--|||I've tried those queries, but cannot get what I want.
Below are tables like the Tables I am using.

For Query1 I want to return
NameHobby
PhilBoxing
AndyBoxing
CiaranBoxing

For Query2 I want to return
NameHobby
PhilAthletics
AndyRugby
CiaranFootball

For Query3 I want to return
NameHobby
PhilFootball
PhilRugby
PhilAthletics
AndyFootball
AndyRugby
AndyAthletics
CiaranFootball
CiaranRugby
CiaranAthletics

TableA

NameHobby
PhilFootball
PhilRugby
AndyAthletics
AndyFootball
CiaranRugby
CiaranAthletics
PhilBoxing
AndyBoxing
CiaranBoxing

TableB
NameHobby
PhilFootball
PhilRugby
PhilAthletics
AndyFootball
AndyRugby
AndyAthletics
CiaranFootball
CiaranRugby
CiaranAthletics
MarkFootball
MarkRugby
MarkAthletics

Regards,
Ciarn|||I've tried those queries, but cannot get what I want.
Below are tables like the Tables I am using.

For Query1 I want to return
Name Hobby
Phil Boxing
Andy Boxing
Ciaran Boxing

For Query2 I want to return
Name Hobby
Phil Athletics
Andy Rugby
Ciaran Football

For Query3 I want to return
Name Hobby
Phil Football
Phil Rugby
Phil Athletics
Andy Football
Andy Rugby
Andy Athletics
Ciaran Football
Ciaran Rugby
Ciaran Athletics

TableA

Name Hobby
Phil Football
Phil Rugby
Andy Athletics
Andy Football
Ciaran Rugby
Ciaran Athletics
Phil Boxing
Andy Boxing
Ciaran Boxing

TableB
Name Hobby
Phil Football
Phil Rugby
Phil Athletics
Andy Football
Andy Rugby
Andy Athletics
Ciaran Football
Ciaran Rugby
Ciaran Athletics
Mark Football
Mark Rugby
Mark Athletics

Regards,
Ciarn|||I think you swapped the first two around from your original but thanks
for the examples. Try:

SELECT B.name, B.hobby
FROM TableB AS B
LEFT JOIN TableA AS A
ON A.name = B.name
AND A.hobby = B.hobby
WHERE A.hobby IS NULL
AND EXISTS
(SELECT *
FROM TableA
WHERE name = B.name)

SELECT A.name, A.hobby
FROM TableA AS A
LEFT JOIN TableB AS B
ON A.name = B.name
AND A.hobby = B.hobby
WHERE B.hobby IS NULL

My third query seems to produce the result you asked for (in a
different order maybe but you just need to add an ORDER BY clause if a
specific order is important). Try it again and let me know. Here's your
sample in code so that you can easily reproduce it. It's always best to
post your table structures and sample data as code so that others can
understand what your tables look like and test out possible solutions:

CREATE TABLE TableA (name VARCHAR(20) NOT NULL, hobby VARCHAR(20) NOT
NULL, PRIMARY KEY (name,hobby))

CREATE TABLE TableB (name VARCHAR(20) NOT NULL, hobby VARCHAR(20) NOT
NULL, PRIMARY KEY (name,hobby))

INSERT INTO TableA (name, hobby)
SELECT 'Phil', 'Football' UNION ALL
SELECT 'Phil', 'Rugby' UNION ALL
SELECT 'Andy', 'Athletics' UNION ALL
SELECT 'Andy', 'Football' UNION ALL
SELECT 'Ciaran', 'Rugby' UNION ALL
SELECT 'Ciaran', 'Athletics' UNION ALL
SELECT 'Phil', 'Boxing' UNION ALL
SELECT 'Andy', 'Boxing' UNION ALL
SELECT 'Ciaran', 'Boxing'

INSERT INTO TableB (name, hobby)
SELECT 'Phil', 'Football' UNION ALL
SELECT 'Phil', 'Rugby' UNION ALL
SELECT 'Phil', 'Athletics' UNION ALL
SELECT 'Andy', 'Football' UNION ALL
SELECT 'Andy', 'Rugby' UNION ALL
SELECT 'Andy', 'Athletics' UNION ALL
SELECT 'Ciaran', 'Football' UNION ALL
SELECT 'Ciaran', 'Rugby' UNION ALL
SELECT 'Ciaran', 'Athletics' UNION ALL
SELECT 'Mark', 'Football' UNION ALL
SELECT 'Mark', 'Rugby' UNION ALL
SELECT 'Mark', 'Athletics'

Hope this helps.

--
David Portas
SQL Server MVP
--|||Absolutely perfect.
Very much appreciated.
Thanks,
Ciarn

3 queries about active/active and multi-instance

Hi,
1. What is the use of Multiple-Instances of Sql server?
2. If I have only 1 Database, which Mode can I use (Active/Active or
Active/Passive) and why?
3. What is the difference between Active/Active and Active/Passive modes
other than the fact that only Active mode holds the shared device at any
given point of time.
Thanks in advance
Pankaj A. Chitriv
Inline.
joe.
"Pankaj" <pankajchitriv@.rediffmail.com(donotspam)> wrote in message
news:E1CE6DD0-D8A1-4B45-BF58-B309F9BAF770@.microsoft.com...
> Hi,
> 1. What is the use of Multiple-Instances of Sql server?
To run multiple instances of SQL Server in the same machine. Look up
multi-instance in BOL for more info. There are trade-offs to running
multiple instances vs. multiple databases in a single instance. Issues like
administration, security, isolation, memory management, real memory size,
etc... are just some that you need to be aware of.

> 2. If I have only 1 Database, which Mode can I use (Active/Active or
> Active/Passive) and why?
Active/passive. A SQL Server database can be driven by 1 and only 1
instance/engine at any time. It is a shared nothing architecture. Suggest
you review MS failover clustering concepts to get a better feel of how this
works. The term active/active really is kind of a marketing term. It really
means both machines are actively doing work on different databases, not both
machines are servicing the same database.
Btw, active/active and active/passive are actually obsolete terms since SQL
Server does support up to 8 nodes now and up to16 instances (not hard limit
but good luck with PSS if you go beyond). Multiple instance cluster is more
appropriate especially when you get into large clusters.

> 3. What is the difference between Active/Active and Active/Passive modes
> other than the fact that only Active mode holds the shared device at any
> given point of time.
Once you understand what active/active and active/passive mean in the SQL
Server world, this will become very clear. That aside, there are significant
considerations for configuration when you have multiple active nodes in a
cluster. You have to be smart about resource allocation and management and
failover strategies.

> Thanks in advance
> Pankaj A. Chitriv
|||Thanks Joe.
Regards
Pankaj
"joe." wrote:

> Inline.
>
> joe.
> "Pankaj" <pankajchitriv@.rediffmail.com(donotspam)> wrote in message
> news:E1CE6DD0-D8A1-4B45-BF58-B309F9BAF770@.microsoft.com...
> To run multiple instances of SQL Server in the same machine. Look up
> multi-instance in BOL for more info. There are trade-offs to running
> multiple instances vs. multiple databases in a single instance. Issues like
> administration, security, isolation, memory management, real memory size,
> etc... are just some that you need to be aware of.
> Active/passive. A SQL Server database can be driven by 1 and only 1
> instance/engine at any time. It is a shared nothing architecture. Suggest
> you review MS failover clustering concepts to get a better feel of how this
> works. The term active/active really is kind of a marketing term. It really
> means both machines are actively doing work on different databases, not both
> machines are servicing the same database.
> Btw, active/active and active/passive are actually obsolete terms since SQL
> Server does support up to 8 nodes now and up to16 instances (not hard limit
> but good luck with PSS if you go beyond). Multiple instance cluster is more
> appropriate especially when you get into large clusters.
> Once you understand what active/active and active/passive mean in the SQL
> Server world, this will become very clear. That aside, there are significant
> considerations for configuration when you have multiple active nodes in a
> cluster. You have to be smart about resource allocation and management and
> failover strategies.
>
>

3 count queries vs UNION ALL

hi all,

i am not sure what would be more optimum for speed, i was wondering if one is better than the other.

i have 3 count queries that i can write as
SELECT COUNT(a) FROM ...
SELECT COUNT(b) FROM ...
SELECT COUNT(c) FROM ...

or i can have them in UNION ALL...

which would be faster three seperate queries or one UNION ALL query, any help?

thanks
Aleksdepends what your UNION ALL query does :)

if you mean this --select 'a' as tablename, count(*) as rows from tablea
union all select 'b', count(*) from tableb
union all select 'c', count(*) from tablecthen this would be faster|||can you explain why?|||let's pretend that you and i are sitting in new york, and i ask you to go and find out how many men, women, and children there are in los angeles

so you get on a plane, go to los angeles, then come back to new york and say "there are X men in los angeles"

then you get on a plane, go to los angeles, then come back to new york and say "there are Y women in los angeles"

finally, you get on a plane, go to los angeles, then come back to new york and say "there are Z children in los angeles"

compare the above to this --

you get on a plane, go to los angeles, then come back to new york and say "there are X men, Y women, and Z children in los angeles"

that's why ;)|||good enough...thanks|||What about other?|||Actually, aside from my poor atteempt at humor...If you did each count in an indeoendent thread and the launched them together so they went off asynchronously, then that might be faster

So, instead of just 1 agent (you flying back and forth in Rudy's example), there are 3 of you flying out at the same time, and instead of you having to doing the counting by yourself, you would have 2 other people doing the counting

but this sounds very much like a class assignement, and I'm sure the instructor wants the answer Rudy gave|||launch asynchronous threads in sql server?

please, do tell how this might be accomplished

i'm betting you lose more of your time (at $NNN/hour) setting this up than you will ever hope to save (at $0.000000000NNN/hour) in machine time over the life of the app|||launch asynchronous threads in sql server?

please, do tell how this might be accomplished

i'm betting you lose more of your time (at $NNN/hour) setting this up than you will ever hope to save (at $0.000000000NNN/hour) in machine time over the life of the app

Are you serious?|||yes, i am

are you?|||Are you saying that having 3 jobs launch at the same time is difficult?|||depends what your UNION ALL query does :)

if you mean this --select 'a' as tablename, count(*) as rows from tablea
union all select 'b', count(*) from tableb
union all select 'c', count(*) from tablecthen this would be fasterJust to muddy the waters further - faster than what?|||I think Ruday means faster than making 3 trips, even using the same spid, like OP suggested

SELECT COUNT(a) FROM ...
SELECT COUNT(b) FROM ...
SELECT COUNT(c) FROM ...

The better question is, what do they want to do with the results|||Are you saying that having 3 jobs launch at the same time is difficult?
it is if you don't know how to "launch" a "job" -- and then, presumably, collate the results, yes?

as i said before, i'm betting you lose more of your time (at $NNN/hour) setting this up than you will ever hope to save (at $0.000000000NNN/hour) in machine time over the life of the app|||With my eyes closed?

Again, it comes down to what they want to do with the results...|||i see things are interesting here...but here is the actual scenario
i am working on mobile device that has a db on an SD card....
currently, i am rewriting a form that someone has previously designed, prior to my work on the form, client complained that the form is very slow during load time. on the form, there is a drop down and 3 label fields. each field is a count. the drop down is a straight select all, however, the other three fields are coming from one SELECT query that does count. I wanted to know what would be faster for me, to do 3 different trips to the database or to use the existing SELECT UNION query...|||could we please see the existing SELECT UNION?|||SELECT 'AREA_COUNT' As ItemTitle, COUNT(AREA_ID) As ItemValue FROM AREA WHERE Area.LEADER_ID = {LEADER} GROUP BY Area.LEADER_ID

UNION

SELECT ju.STEP_CODE + '_COUNT' As ItemTitle, COUNT(distinct a.AREA_ID) As ItemValue FROM AREA a INNER JOIN JOB_UNIT ju ON a.AREA_ID = ju.AREA_ID AND ju.COMPLETE_DATE IS NULL INNER JOIN EMPLOYEE_WORK sw ON ju.JOB_UNIT_ID = sw.JOB_UNIT_ID AND sw.REASSIGN_REASON_CODE IS NULL WHERE Area.LEADER_ID = {LEADER} GROUP BY ju.STEP_CODE

UNION

SELECT 'UNASSIGNED_COUNT' As ItemTitle, COUNT(a.AREA_ID) As ItemValue FROM AREA a INNER JOIN USER_CREDENTIAL uc ON RECORD_ID = 0 INNER JOIN EMPLOYEE s ON uc.FDCA_EMPLOYEE_ID = s.FDCA_EMPLOYEE_ID INNER JOIN JOB_UNIT ju ON a.AREA_ID = ju.AREA_ID AND s.FLD_OPCODE = ju.FLD_OPCODE AND ju.COMPLETE_DATE IS NULL LEFT OUTER JOIN EMPLOYEE_WORK sw ON ju.JOB_UNIT_ID = sw.JOB_UNIT_ID AND sw.REASSIGN_REASON_CODE IS NULL WHERE sw.JOB_UNIT_ID IS NULL AND Area.LEADER_ID = {LEADER} GROUP BY Area.LEADER_ID

UNION

SELECT 'COMPLETE_COUNT' As ItemTitle, COUNT(a.AREA_ID) As ItemValue FROM AREA a INNER JOIN USER_CREDENTIAL uc ON RECORD_ID = 0 INNER JOIN EMPLOYEE s ON uc.FDCA_EMPLOYEE_ID = s.FDCA_EMPLOYEE_ID INNER JOIN JOB_UNIT ju ON a.AREA_ID = ju.AREA_ID AND s.FLD_OPCODE = ju.FLD_OPCODE AND ju.COMPLETE_DATE IS NOT NULL WHERE Area.LEADER_ID = {LEADER} GROUP BY Area.LEADER_ID|||more efficient would be to use UNION ALL so sql doesn't have to sort. but it will be a negligible difference. you'd notice it more if each query were returning many rows.

in general it's better to use UNION ALL instead of UNION if you can get away with it though.|||FYI when you do this --

... WHERE xxx.LEADER_ID = {LEADER} GROUP BY xxx.LEADER_ID

then if xxx.LEADER_ID is not mentioned in the SELECT, you can go ahead and eliminate the GROUP BY clause|||What kinda join is this?

FROM AREA a
INNER JOIN USER_CREDENTIAL uc
ON RECORD_ID = 0|||Yes remove to group by and convert the union's

What does the show plan say? Do you have any index or table scans?

SELECT 'AREA_COUNT' As ItemTitle
, COUNT(AREA_ID) As ItemValue
FROM AREA
WHERE Area.LEADER_ID = {LEADER}
--GROUP BY Area.LEADER_ID
UNION ALL
SELECT ju.STEP_CODE + '_COUNT' As ItemTitle
, COUNT(distinct a.AREA_ID) As ItemValue
FROM AREA a
INNER JOIN JOB_UNIT ju
ON a.AREA_ID = ju.AREA_ID
AND ju.COMPLETE_DATE IS NULL
INNER JOIN EMPLOYEE_WORK sw
ON ju.JOB_UNIT_ID = sw.JOB_UNIT_ID
AND sw.REASSIGN_REASON_CODE IS NULL
WHERE Area.LEADER_ID = {LEADER}
--GROUP BY ju.STEP_CODE
UNION ALL
SELECT 'UNASSIGNED_COUNT' As ItemTitle
, COUNT(a.AREA_ID) As ItemValue
FROM AREA a
INNER JOIN USER_CREDENTIAL uc
ON RECORD_ID = 0
INNER JOIN EMPLOYEE s
ON uc.FDCA_EMPLOYEE_ID = s.FDCA_EMPLOYEE_ID
INNER JOIN JOB_UNIT ju
ON a.AREA_ID = ju.AREA_ID
AND s.FLD_OPCODE = ju.FLD_OPCODE
AND ju.COMPLETE_DATE IS NULL
LEFT JOIN EMPLOYEE_WORK sw
ON ju.JOB_UNIT_ID = sw.JOB_UNIT_ID
AND sw.REASSIGN_REASON_CODE IS NULL
WHERE sw.JOB_UNIT_ID IS NULL
AND Area.LEADER_ID = {LEADER}
--GROUP BY Area.LEADER_ID
UNION ALL
SELECT 'COMPLETE_COUNT' As ItemTitle
, COUNT(a.AREA_ID) As ItemValue
FROM AREA a
INNER JOIN USER_CREDENTIAL uc
ON RECORD_ID = 0
INNER JOIN EMPLOYEE s
ON uc.FDCA_EMPLOYEE_ID = s.FDCA_EMPLOYEE_ID
INNER JOIN JOB_UNIT ju
ON a.AREA_ID = ju.AREA_ID
AND s.FLD_OPCODE = ju.FLD_OPCODE
AND ju.COMPLETE_DATE IS NOT NULL
WHERE Area.LEADER_ID = {LEADER}
--GROUP BY Area.LEADER_ID|||indexes are in place for all the primary fields in the tables...i am going to go with UNION ALL, after all there is a lot of data...thank you all|||I'm still at a loss to see what those group by's would do|||well, it seems that i have to use GROUP BY, I am getting an error when I try to compile...when i do use the group by clause the error goes away...

the error i am getting is Invalid Expression|||when i do use the group by clause the error goes away...one of the best. reasons. ever.

say, what about that interesting join that brett asked you about?|||well, it seems that i have to use GROUP BY, I am getting an error when I try to compile...when i do use the group by clause the error goes away...

the error i am getting is Invalid Expression

Well that's not a good reason...

Post the DDL of your tables

And the indexes (or is that indicies?)|||well, it seems that i have to use GROUP BY, I am getting an error when I try to compile...when i do use the group by clause the error goes away...

another way to achieve error free execution is to comment out the whole query.

it would also run faster that way.|||another way to achieve error free execution is to comment out the whole query.

it would also run faster that way.

U FUNNY

But don't give up the day job just yet|||I actually had a boss one time that fancied himself an expert in t-sql.

He once claimed he fixed a bug in one of his procs by removing a comment.

I don't work there anymore thankfully. the company has since popped.|||I just wonder what kind of meaning that queries has...look at this sample

USE Northwind

SELECT 'Label1' AS Source, COUNT(*)
FROM orders
GROUP BY CustomerID
UNION ALL
SELECT 'Label2' AS Source, COUNT(*)
FROM [order details]
GROUP BY ProductID

Kind of the same thing he''s doig...no?

Thursday, February 16, 2012

2005 Fully qualified names.

In SQL 2005 I have a server with a username of JM. This username created and
maintains a database. When I login via query analyser I can`t run queries on
a table unless I qualify it JM.tablename. How do I alter the login to ensure
i can query just on tablename ?
Simon
But I`m logging onto query analyzer as JM. So I would expect not to have to
qualify the name. If I changed the database ownership to dbo then wouldn`t
that ensure that I`d have to use dbo.tablename ?
Si
"vt" wrote:

> Hi
> This is because JM is the owner of the object ,
> use sp_changeobjectowner to change the owner form jm to dbo
> e.g
> sp_changeobjectowner 'Table' , 'dbo'
>
> Regards
> VT
> Knowledge is power, share it...
> http://oneplace4sql.blogspot.com/
> "Simon" <Simon@.discussions.microsoft.com> wrote in message
> news:2CFA854D-47A1-4676-B80D-47341B87D408@.microsoft.com...
>
>
|||Simon
In SQL Server 2005 MS has introduced SCHEMA that all database objects belong
to. Think about a container that holds objects.
You will have to be a memeber of sysadmin server role as well as db_owner
database role.
"Simon" <Simon@.discussions.microsoft.com> wrote in message
news:2CFA854D-47A1-4676-B80D-47341B87D408@.microsoft.com...
> In SQL 2005 I have a server with a username of JM. This username created
> and
> maintains a database. When I login via query analyser I can`t run queries
> on
> a table unless I qualify it JM.tablename. How do I alter the login to
> ensure
> i can query just on tablename ?
> Simon
|||Ok thats cool, I understand this and have changed my ownership accordingly.
Is there an easy way to alter the qualified names in strored procs and views ?
Si
"Simon" wrote:

> In SQL 2005 I have a server with a username of JM. This username created and
> maintains a database. When I login via query analyser I can`t run queries on
> a table unless I qualify it JM.tablename. How do I alter the login to ensure
> i can query just on tablename ?
> Simon
|||It is a good idea to always schema qualify all objects anyway.
Andrew J. Kelly SQL MVP
"Simon" <Simon@.discussions.microsoft.com> wrote in message
news:2CFA854D-47A1-4676-B80D-47341B87D408@.microsoft.com...
> In SQL 2005 I have a server with a username of JM. This username created
> and
> maintains a database. When I login via query analyser I can`t run queries
> on
> a table unless I qualify it JM.tablename. How do I alter the login to
> ensure
> i can query just on tablename ?
> Simon
|||I teach my clients that it is MANDATORY to qualify every database object.
:-) There is just no valid reason not to IMHO, and it does save the engine
some effort.
TheSQLGuru
President
Indicium Resources, Inc.
"Andrew J. Kelly" <sqlmvpnooospam@.shadhawk.com> wrote in message
news:ujJxPjYuHHA.3480@.TK2MSFTNGP04.phx.gbl...
> It is a good idea to always schema qualify all objects anyway.
> --
> Andrew J. Kelly SQL MVP
> "Simon" <Simon@.discussions.microsoft.com> wrote in message
> news:2CFA854D-47A1-4676-B80D-47341B87D408@.microsoft.com...
>
|||I guess I understated that some. I always tell people to qualify objects
no matter what as well.
Andrew J. Kelly SQL MVP
"TheSQLGuru" <kgboles@.earthlink.net> wrote in message
news:eX7ZDnZuHHA.5028@.TK2MSFTNGP02.phx.gbl...
>I teach my clients that it is MANDATORY to qualify every database object.
>:-) There is just no valid reason not to IMHO, and it does save the engine
>some effort.
> --
> TheSQLGuru
> President
> Indicium Resources, Inc.
> "Andrew J. Kelly" <sqlmvpnooospam@.shadhawk.com> wrote in message
> news:ujJxPjYuHHA.3480@.TK2MSFTNGP04.phx.gbl...
>

2005 Fully qualified names.

In SQL 2005 I have a server with a username of JM. This username created and
maintains a database. When I login via query analyser I can`t run queries on
a table unless I qualify it JM.tablename. How do I alter the login to ensure
i can query just on tablename ?
SimonHi
This is because JM is the owner of the object ,
use sp_changeobjectowner to change the owner form jm to dbo
e.g
sp_changeobjectowner 'Table' , 'dbo'
Regards
VT
Knowledge is power, share it...
http://oneplace4sql.blogspot.com/
"Simon" <Simon@.discussions.microsoft.com> wrote in message
news:2CFA854D-47A1-4676-B80D-47341B87D408@.microsoft.com...
> In SQL 2005 I have a server with a username of JM. This username created
> and
> maintains a database. When I login via query analyser I can`t run queries
> on
> a table unless I qualify it JM.tablename. How do I alter the login to
> ensure
> i can query just on tablename ?
> Simon|||But I`m logging onto query analyzer as JM. So I would expect not to have to
qualify the name. If I changed the database ownership to dbo then wouldn`t
that ensure that I`d have to use dbo.tablename ?
Si
"vt" wrote:

> Hi
> This is because JM is the owner of the object ,
> use sp_changeobjectowner to change the owner form jm to dbo
> e.g
> sp_changeobjectowner 'Table' , 'dbo'
>
> Regards
> VT
> Knowledge is power, share it...
> http://oneplace4sql.blogspot.com/
> "Simon" <Simon@.discussions.microsoft.com> wrote in message
> news:2CFA854D-47A1-4676-B80D-47341B87D408@.microsoft.com...
>
>|||Simon
In SQL Server 2005 MS has introduced SCHEMA that all database objects belong
to. Think about a container that holds objects.
You will have to be a memeber of sysadmin server role as well as db_owner
database role.
"Simon" <Simon@.discussions.microsoft.com> wrote in message
news:2CFA854D-47A1-4676-B80D-47341B87D408@.microsoft.com...
> In SQL 2005 I have a server with a username of JM. This username created
> and
> maintains a database. When I login via query analyser I can`t run queries
> on
> a table unless I qualify it JM.tablename. How do I alter the login to
> ensure
> i can query just on tablename ?
> Simon|||Ok thats cool, I understand this and have changed my ownership accordingly.
Is there an easy way to alter the qualified names in strored procs and views
?
Si
"Simon" wrote:

> In SQL 2005 I have a server with a username of JM. This username created a
nd
> maintains a database. When I login via query analyser I can`t run queries
on
> a table unless I qualify it JM.tablename. How do I alter the login to ensu
re
> i can query just on tablename ?
> Simon|||It is a good idea to always schema qualify all objects anyway.
Andrew J. Kelly SQL MVP
"Simon" <Simon@.discussions.microsoft.com> wrote in message
news:2CFA854D-47A1-4676-B80D-47341B87D408@.microsoft.com...
> In SQL 2005 I have a server with a username of JM. This username created
> and
> maintains a database. When I login via query analyser I can`t run queries
> on
> a table unless I qualify it JM.tablename. How do I alter the login to
> ensure
> i can query just on tablename ?
> Simon|||I teach my clients that it is MANDATORY to qualify every database object.
:-) There is just no valid reason not to IMHO, and it does save the engine
some effort.
TheSQLGuru
President
Indicium Resources, Inc.
"Andrew J. Kelly" <sqlmvpnooospam@.shadhawk.com> wrote in message
news:ujJxPjYuHHA.3480@.TK2MSFTNGP04.phx.gbl...
> It is a good idea to always schema qualify all objects anyway.
> --
> Andrew J. Kelly SQL MVP
> "Simon" <Simon@.discussions.microsoft.com> wrote in message
> news:2CFA854D-47A1-4676-B80D-47341B87D408@.microsoft.com...
>|||I guess I understated that some. I always tell people to qualify objects
no matter what as well.
Andrew J. Kelly SQL MVP
"TheSQLGuru" <kgboles@.earthlink.net> wrote in message
news:eX7ZDnZuHHA.5028@.TK2MSFTNGP02.phx.gbl...
>I teach my clients that it is MANDATORY to qualify every database object.
>:-) There is just no valid reason not to IMHO, and it does save the engine
>some effort.
> --
> TheSQLGuru
> President
> Indicium Resources, Inc.
> "Andrew J. Kelly" <sqlmvpnooospam@.shadhawk.com> wrote in message
> news:ujJxPjYuHHA.3480@.TK2MSFTNGP04.phx.gbl...
>