Having some trouble with this select statement...

MS SQL SERVER

    Next

  • 1. Connectivity in between SQL Server and DB2
    I would like to know whether a connection can established in between SQL Server and DB2. If yes how?
  • 2. Scripting Profiler Traces
    It looks as though when you script a trace in SQL 2000 or 2005 you cannot save the results of the trace in a table. You were able to do this in SQL 7.0. Anybody know why they got rid of this feature and if there is a workaround?
  • 3. what date is missing from a range of dates
    ok What I need to do is find out who hasn't subitted a time sheet entry and what was the missing date. the table has amond other things a userid and datesubmitted field I would like to have a form the where I can either select two dates which would return a list of userID and dates for the missing timesheets thanks Marcel
  • 4. troubles with triggers
    I have an instead of delete trigger that prevents some rows a user might have selected to delete from being deleted. I also have a stored procedure that is called from the client to delete selected rows. The trigger is supposed to prevent the procedure from accidentally deleting rows that should not be. If I try to delete a row in EM my trigger works. I'm warned and the delete does not occur. Also, if I run something like "Delete From Table1" in QA the trigger also works just fine. But if I run the procedure that is normally used to delete rows it seems that the trigger runs but not correctly. If I need to post more detail I will be glad to do that but I'm just wondering at first if there is some setting I need to use in a stored procedure that will allow triggers to work properly. Is there something I may have set or is set by default to cause the problem I'm experiencing? Thanks, Keith
  • 5. behavior of command in the 'SQL Query Analyzer'
    Start by generating a script file in Enterprise Manager. Include all stored procedures and views. Script as CREATE, but do NOT include the DELETE! Now edit the script, and change all the CREATE PROC and CREATE VIEW to ALTER commands. Then run the script. Run it a few times, if you want. I believe the dependencies should be up to date. (Personally I don't pay any attention to those dependencies, but that is partly because I formed my habits before they introduced ALTER!) Roy On 27 Feb 2006 07:22:50 -0800, "dbuchanan" < XXXX@XXXXX.COM > wrote: >Dan > >Thank you. > >Is there any way, maybe some command that I can use, to identify those >objects that are not up to date? > >'sp_depends' seems kind of worthless if information must be accounted >for 'manually' in order for the commands to work. > >dbuchanan

Re: Having some trouble with this select statement...

Postby Arnie Rowland » Fri, 30 Jun 2006 04:52:17 GMT

his is a multi-part message in MIME format.


Your query doesn't look quite right...

Should it be more like:

SELECT
e.Employeeid
, e.EmpID
, s.OrgNode
FROM tblEmployee e
JOIN tblEmployeeOrgNode o
ON e.EmpId = o.EmpID
JOIN tblOrgSystemNode s
ON ( s.OrgNOdeId = a.OrgNodeID
AND s.OrgSystemID = a.OrdSystemID
)

--( Are OrgNodes uniquely identified, or --are they paired with OrgSystem?)

This is just a guess. While sending the DDL and your expected results is excellent, also having a sense of the current data that leads to the results would be beneficial.

--
Arnie Rowland, YACE*
"To be successful, your heart must accompany your knowledge."

*Yet Another Certification Exam


"rhaazy" < XXXX@XXXXX.COM > wrote in message news: XXXX@XXXXX.COM ...
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML><HEAD>
<META http-equiv=Content-Type content="text/html; charset=iso-8859-1">
<META content="MSHTML 6.00.5296.0" name=GENERATOR>
<STYLE></STYLE>
</HEAD>
<BODY>
<DIV><FONT face=Arial size=2>Your query doesn't look quite right...</FONT></DIV>
<DIV><FONT face=Arial size=2></FONT> </DIV>
<DIV><FONT face=Arial size=2>Should it be more like:</FONT></DIV>
<DIV><FONT face=Arial size=2></FONT> </DIV>
<DIV><FONT face="Courier New" size=2>SELECT <BR>    
e.Employeeid<BR>   , e.EmpID<BR>   , s.OrgNode<BR>FROM
tblEmployee e <BR>   JOIN
tblEmployeeOrgNode o<BR>      ON e.EmpId =
o.EmpID<BR>   JOIN
tblOrgSystemNode s <BR>       ON
(   s.OrgNOdeId = a.OrgNodeID</FONT></DIV>
<DIV><FONT face="Courier New"
size=2>      </FONT><FONT face="Courier New"
size=2>    AND s.OrgSystemID = a.OrdSystemID 
</FONT></DIV>
<DIV><FONT face="Courier New"
size=2>          )</FONT></DIV>
<DIV><FONT face="Courier New" size=2></FONT> </DIV>
<DIV><FONT face="Courier New" size=2>--( Are OrgNodes uniquely identified, or
--are they paired with OrgSystem?)</FONT></DIV>
<DIV><FONT face="Courier New" size=2></FONT> </DIV>
<DIV><FONT face="Courier New" size=2>This is just a guess. While sending the DDL
and your expected results is excellent, also having a sense of the current data
that leads to the results would be beneficial.</FONT></DIV>
<DIV><FONT face=Arial size=2></FONT> </DIV>
<DIV><FONT face=Arial size=2>-- <BR>Arnie Rowland, YACE* <BR>"To be successful,
your heart must accompany your knowledge."</FONT></DIV>
<DIV><FONT face=Arial size=2></FONT> </DIV>
<DIV><FONT face=Arial size=2>*Yet Another Certification Exam</FONT></DIV>
<DIV><FONT face=Arial size=2></FONT> </DIV>
<DIV><FONT face=Arial size=2></FONT> </DIV>
<DIV><FO

Re: Having some trouble with this select statement...

Postby rhaazy » Fri, 30 Jun 2006 05:06:46 GMT

hat you have looks to be correct, however I dont know where you got
the prefix 'a' from.... I'm a little new to sql so I'm having trouble
figuring out how to get that other alias.

Arnie Rowland wrote:


Re: Having some trouble with this select statement...

Postby rhaazy » Fri, 30 Jun 2006 05:09:11 GMT

ey Nevermind I got it figured out, your post helped a lot thanks very
much!!
Arnie Rowland wrote:


Re: Having some trouble with this select statement...

Postby Steve Kass » Fri, 30 Jun 2006 05:15:54 GMT

A wild guess - maybe the primary key of tblOrgSystemNode is
(OrgSystemID,OrgNodeID), in which case you might need

select Employeeid, tblEmployee.EmpID, OrgNode
from tblEmployee JOIN tblEmployeeOrgNode a
on tblEmployee.EmpId = a.EmpID,
tblOrgSystemNode JOIN tblEmployeeOrgNode b
on tblOrgSystemNode.OrgNOdeId = b.OrgNodeID
and tblOrgSystemNode.OrgSystemID= b.OrgSystemID

However, I suggest you first make sure you understand how
your data is modeled in these tables.  Even if my wild guess gives
you the results you want, it doesn't mean the query asks the
question you intend to ask.  To be sure, you need to know what
information in your tables keeps track of the association between
an employee and a [Location].  (And I'm ignoring the fact that
the query you posted does not have a [Location] column, even
though the results you posted do.)

-- Steve Kass
-- Drew University
--  http://www.**--****.com/ 
-- 23900E82-EE93-4138-8049-C1DFAD2D0E16






Re: Having some trouble with this select statement...

Postby rhaazy » Fri, 30 Jun 2006 07:29:09 GMT

Yes, this is what I required, I thought what I posted was sufficient
but next time I will know to go more in depth, thanks for your help.






Re: Having some trouble with this select statement...

Postby Arnie Rowland » Fri, 30 Jun 2006 09:34:19 GMT

ypo - 'a' should have been 'o'.

--
Arnie Rowland, YACE*
"To be successful, your heart must accompany your knowledge."

*Yet Another Certification Exam


"rhaazy" < XXXX@XXXXX.COM > wrote in message
news: XXXX@XXXXX.COM ...



Similar Threads:

1.Having trouble with this create table statement

hi

sql server 2005

When I insert a row into this table I get this error:
The INSERT statement conflicted with the FOREIGN KEY SAME TABLE constraint

the idea of having this self referecning was to tell if a comment is a reply 
to another comment. A comment doesn't need to be a reply to anther comment.

CREATE table dbo.Comment(
 Id uniqueidentifier NOT NULL,
 UserId uniqueidentifier NOT NULL,
 CommentId uniqueidentifier NULL,
 RegDate datetime NOT NULL CONSTRAINT DF_CommentDate  DEFAULT (getdate()),
 Body nvarchar(1000),
 CONSTRAINT PK_Comment PRIMARY KEY CLUSTERED (id ASC),
 CONSTRAINT FK_Comment_Aspnet_Users FOREIGN KEY(UserId) REFERENCES 
dbo.aspnet_Users (UserId),
 CONSTRAINT FK_Comment_Comment FOREIGN KEY(CommentId) REFERENCES 
dbo.Comment(Id)
)

any suggestion? 


2.Having trouble with SELECT UNION

I have created the following VIEWS in my database. (Sorry, they're quite
long, but I've reproduced them exactly below.) In a nutshell,
VMSGRECIPIENTSA and VMSGRECIPIENTSB return the same record structure.
VMSGRECIPIENTS simply pulls these two together via a UNION. (For brevity,
I'n going to call these VMRA, VMRB and VMR.)

A bit of history: Orignally, VMRA and VMRB did not exist. VMR was crafted as
a single union query with all the complexity of VMRA and VMRB UNIONed
together. However, I had performance problems. I began to split up the VIEW
to see which of the UNIONed SELECTS was causing the problem. Interestingly
enough, I found that each on its own ran like lightning. I also found that

SELECT * FROM VMRA UNION SELECT * FROM VMRB WHERE ... also ran like
lightning. OK, sez I. I'll play that game. I split the two unioned SELECT
statments into VMRA and VMRB and crafted VMR to simply UNION these together.
Sounds nice, but it just doesn't work out that way.

At this point SELECT * FROM VMRA UNION SELECT * FROM VMRB WHERE ... runs
like lightning. (As we already saw.) However, SELECT * FROM VMR WHERE ...
(same WHERE CLAUSE) takes forever to execute. It's as though in processing
VMR, the DBMS *first* UNIONs together all records qualified in the two
SELECTs (what a dog!) and only *then* applies the WHERE clause to select the
qualifying records from the UNIONed recordset which it has created. This
just won't fly. When retrieving from a VIEW, shouldn't the WHERE criteria be
applied as early on as possible?

DISCLAIMER: I am not using SQL Server at the moment, I'm using Interbase. So
how can you help me? Well first of all, if you're familiar with this sort of
thing and you can tell me, 'yeah dummy, that's how it works ;-)' well,
that'll be a big help to me. On the other hand, if you can tell me that SQL
Server would handle this just fine, and that it's Interbase which is
brain-dead, well I might not have a solution, but I'll feel just that much
better for crafting this UNION in this first place.

So in the final analysis, what is the practical application for UNION? Is
UNION intended specifically for submitted SELECT statements (which can
contain qualifying selection criteria) as opposed to for VIEWS (which don't
contain qualifying selection criteria). Is UNION not suitable for VIEW
definitions?

(VIEW definitions presented below.)

Thanks very much for your help!

- Joseph Geretz -

CREATE VIEW "VMSGRECIPIENTSA" (
  "MSGID",
  "MSGTYPE",
  "MSGSTATUS",
  "OPENEDTIME",
  "SEALEDTIME",
  "PERSONID",
  "SHAREDID",
  "ATTACHMENTS",
  "ATTACHMENTTYPE",
  "ROUTINGID",
  "ROUTINGTYPE",
  "RESPONSETOID",
  "FROMID",
  "SUBJECTID",
  "SUBJECT",
  "ADDLSUBJECT",
  "PRIORITYID",
  "PRIORITYCODE",
  "PRIORITYSORT",
  "MSGTEXT",
  "RECIPIDDIRECT",
  "ISPOOL",
  "RECIPIDPOOL",
  "ORIGPOOLID",
  "ISRESPONSIBLE",
  "ISPRIVATE",
  "ISRECEIPTREQ",
  "LASTACTION",
  "CREATETIME",
  "SCHEDTIME",
  "UPDATETIME"
) AS

SELECT
  MSGMASTER.MSGID,
  MSGMASTER.MSGTYPE,
  MSGMASTER.MSGSTATUS,
  MSGMASTER.CREATETIME,
  MSGMASTER.SEALEDTIME,
  MSGMASTER.PERSONID,
  PERSON.SHAREDID,
  VMSGATTACHMENTS.ATTACHMENTS,
  DOCUMENTS.DOCUMENTTYPEID,
  MSGROUTING.ROUTINGID,
  MSGROUTING.ROUTINGTYPE,
  MSGROUTING.RESPONSETOID,
  MSGROUTING.USERSECURITYID,
  MSGROUTING.SUBJECTID,
  MSGSUBJECTS.SUBJECT,
  MSGROUTING.ADDLSUBJECT,
  MSGROUTING.PRIORITYID,
  MSGPRIORITIES.PRIORITYCODE,
  MSGPRIORITIES.SORTSEQ,
  MSGROUTING.MSGTEXT,
  MSGRECIPIENTS.ENTITYID,
  USERGROUPS.ISPOOL,
  GROUPMEMBERSHIP.USERSECURITYID,
  MSGRECIPIENTS.ORIGGROUPID,
  MSGRECIPIENTS.ISRESPONSIBLE,
  MSGRECIPIENTS.ISPRIVATE,
  MSGRECIPIENTS.ISRECEIPTREQ,
  MSGRECIPIENTS.LASTACTION,
  MSGROUTING.CREATETIME,
  MSGRECIPIENTS.SCHEDTIME,
  MSGRECIPIENTS.UPDATETIME
FROM
  MSGMASTER
  LEFT JOIN PERSON ON (MSGMASTER.PERSONID = PERSON.PERSONID)
  LEFT JOIN MSGROUTING ON (MSGROUTING.MSGID = MSGMASTER.MSGID)
  LEFT JOIN MSGRECIPIENTS ON (MSGRECIPIENTS.MSGID = MSGROUTING.MSGID) AND
(MSGRECIPIENTS.ROUTINGID =

MSGROUTING.ROUTINGID)
  LEFT JOIN USERGROUPS ON (USERGROUPS.GROUPID = MSGRECIPIENTS.ENTITYID)
  LEFT JOIN GROUPMEMBERSHIP ON (GROUPMEMBERSHIP.GROUPID =
USERGROUPS.GROUPID)
  LEFT JOIN MSGPRIORITIES ON (MSGROUTING.PRIORITYID =
MSGPRIORITIES.PRIORITYID)
  LEFT JOIN MSGSUBJECTS ON (MSGROUTING.SUBJECTID = MSGSUBJECTS.SUBJECTID)
  LEFT JOIN VMSGATTACHMENTS ON (MSGROUTING.MSGID = VMSGATTACHMENTS.MSGID)
  LEFT JOIN  DOCUMENTS ON (VMSGATTACHMENTS.ATTACHMENTS =
DOCUMENTS.DOCUMENTID)
WHERE (MSGROUTING.SUBJECTID <> 0)
;
COMMIT WORK;

/* View: VMSGRECIPIENTSB, Owner: SRSSOFTWARE */

CREATE VIEW "VMSGRECIPIENTSB" (
  "MSGID",
  "MSGTYPE",
  "MSGSTATUS",
  "OPENEDTIME",
  "SEALEDTIME",
  "PERSONID",
  "SHAREDID",
  "ATTACHMENTS",
  "ATTACHMENTTYPE",
  "ROUTINGID",
  "ROUTINGTYPE",
  "RESPONSETOID",
  "FROMID",
  "SUBJECTID",
  "SUBJECT",
  "ADDLSUBJECT",
  "PRIORITYID",
  "PRIORITYCODE",
  "PRIORITYSORT",
  "MSGTEXT",
  "RECIPIDDIRECT",
  "ISPOOL",
  "RECIPIDPOOL",
  "ORIGPOOLID",
  "ISRESPONSIBLE",
  "ISPRIVATE",
  "ISRECEIPTREQ",
  "LASTACTION",
  "CREATETIME",
  "SCHEDTIME",
  "UPDATETIME"
) AS

SELECT
  MSGMASTER.MSGID,
  MSGMASTER.MSGTYPE,
  MSGMASTER.MSGSTATUS,
  MSGMASTER.CREATETIME,
  MSGMASTER.SEALEDTIME,
  MSGMASTER.PERSONID,
  PERSON.SHAREDID,
  VMSGATTACHMENTS.ATTACHMENTS,
  DOCUMENTS.DOCUMENTTYPEID,
  MSGROUTING.ROUTINGID,
  MSGROUTING.ROUTINGTYPE,
  MSGROUTING.RESPONSETOID,
  MSGROUTING.USERSECURITYID,
  MSGROUTING.SUBJECTID,
  MSGROUTING.ADDLSUBJECT,
  MSGROUTING.ADDLSUBJECT,
  MSGROUTING.PRIORITYID,
  MSGPRIORITIES.PRIORITYCODE,
  MSGPRIORITIES.SORTSEQ,
  MSGROUTING.MSGTEXT,
  MSGRECIPIENTS.ENTITYID,
  USERGROUPS.ISPOOL,
  GROUPMEMBERSHIP.USERSECURITYID,
  MSGRECIPIENTS.ORIGGROUPID,
  MSGRECIPIENTS.ISRESPONSIBLE,
  MSGRECIPIENTS.ISPRIVATE,
  MSGRECIPIENTS.ISRECEIPTREQ,
  MSGRECIPIENTS.LASTACTION,
  MSGROUTING.CREATETIME,
  MSGRECIPIENTS.SCHEDTIME,
  MSGRECIPIENTS.UPDATETIME
FROM
  MSGMASTER
  LEFT JOIN PERSON ON (MSGMASTER.PERSONID = PERSON.PERSONID)
  LEFT JOIN MSGROUTING ON (MSGROUTING.MSGID = MSGMASTER.MSGID)
  LEFT JOIN MSGRECIPIENTS ON (MSGRECIPIENTS.MSGID = MSGROUTING.MSGID) AND
(MSGRECIPIENTS.ROUTINGID =

MSGROUTING.ROUTINGID)
  LEFT JOIN USERGROUPS ON (USERGROUPS.GROUPID = MSGRECIPIENTS.ENTITYID)
  LEFT JOIN GROUPMEMBERSHIP ON (GROUPMEMBERSHIP.GROUPID =
USERGROUPS.GROUPID)
  LEFT JOIN MSGPRIORITIES ON (MSGROUTING.PRIORITYID =
MSGPRIORITIES.PRIORITYID)
  LEFT JOIN MSGSUBJECTS ON (MSGROUTING.SUBJECTID = MSGSUBJECTS.SUBJECTID)
  LEFT JOIN VMSGATTACHMENTS ON (MSGROUTING.MSGID = VMSGATTACHMENTS.MSGID)
  LEFT JOIN  DOCUMENTS ON (VMSGATTACHMENTS.ATTACHMENTS =
DOCUMENTS.DOCUMENTID)
WHERE (MSGROUTING.SUBJECTID = 0)
;
COMMIT WORK;

/* View: VMSGRECIPIENTS, Owner: SRSSOFTWARE */

CREATE VIEW "VMSGRECIPIENTS" (
  "MSGID",
  "MSGTYPE",
  "MSGSTATUS",
  "OPENEDTIME",
  "SEALEDTIME",
  "PERSONID",
  "SHAREDID",
  "ATTACHMENTS",
  "ATTACHMENTTYPE",
  "ROUTINGID",
  "ROUTINGTYPE",
  "RESPONSETOID",
  "FROMID",
  "SUBJECTID",
  "SUBJECT",
  "ADDLSUBJECT",
  "PRIORITYID",
  "PRIORITYCODE",
  "PRIORITYSORT",
  "MSGTEXT",
  "RECIPIDDIRECT",
  "ISPOOL",
  "RECIPIDPOOL",
  "ORIGPOOLID",
  "ISRESPONSIBLE",
  "ISPRIVATE",
  "ISRECEIPTREQ",
  "LASTACTION",
  "CREATETIME",
  "SCHEDTIME",
  "UPDATETIME"
) AS

SELECT * FROM VMSGRECIPIENTSA
   UNION
SELECT * FROM VMSGRECIPIENTSB
;
COMMIT WORK;


3.having problem in subquery in select statement

Hi,
i have a query which run successfully on SQL Server but giving error in
SQLCE.
The Query is:

SELECT a.Ad_Surr_Key, a.Ad_Description, count(*) as WO_Count,
(select count(*) from tbl_WORK_ORDER_HEADER wx, tbl_WORK_ORDER_DETAIL
wdx
where wx.WO_Hdr_Surr_Key = wdx.WO_Hdr_Surr_Key and wdx.Ad_Surr_Key =
a.Ad_Surr_Key and
(WO_Status = '0' OR WO_Status = '2')) as Count_Inc from
 tbl_WORK_ORDER_HEADER w, tbl_WORK_ORDER_DETAIL wd, tbl_UNIT_MASTER u,
 tbl_AD_MASTER a, tbl_CREW_MASTER c, tbl_ROUTE_MASTER r where
w.WO_Hdr_Surr_Key = wd.WO_Hdr_Surr_Key and wd.Ad_Surr_Key =
a.Ad_Surr_Key and
 wd.Assigned_Crew_Surr_Key = c.Crew_Surr_Key and c.Crew_Surr_Key = '1'
and
 w.Unit_Surr_Key = u.Unit_Surr_Key and u.Route_Surr_Key =
r.Route_Surr_Key and
r.Route_Surr_Key = '2'
group by a.Ad_Surr_Key, a.Ad_Description order by a.Ad_Description

can anyone have solution.

4.Trouble with SELECT ... INTO SQL statement

Hi all.

I'm trying to use some VBA to browse through the Tables of my Access DB and 
to create Back-ups in a separate backup Access file for the ones i want.

To achieve the latter, i'm trying to use an SQL SELECT ... INTO statement 
identical to the one that's being created if i design a Make-Table Query.

If i design and run the Query, it's being executed ok and the Table i 
include in the Query design is indeed created in the backup Access file.
However, if i try to use the Query's SQL statement to execute the same 
action via VBA, it fails.

Here's the related part of my VBA code:

strSQL = "SELECT [" & BackupTbls(iCnt) & "].* " _
            & "INTO [" & BackupTbls(iCnt) & "] IN '" & strBackupDB & "' " _
            & "FROM [" & BackupTbls(iCnt) & "]"
CurrentDb.Connection.Execute strSQL

Notes:
- BackupTbls() is a String type array containing the Tables i want to backup
- strBackupDB is the full name of the backup Access file (Path + Name)
- The strSQL string is IDENTICAL to the one used by the Access Query (except 
for the ';' in the end in the Query string)

The error message im getting is:

Run-time error '3251'
Operation is not supported for this type of object.

This appears only when trying to execute via VBA, the normal Access Query 
(with the identical SQL string) executes ok.

I'd be grateful to have any suggestions on this.

Kind regards,
Alex



5.Use results from a select statement in a follow-up select statement in a SP

Hi,

This is probably a simple question, but I'm new at this.  What I am
trying to do is use the results from one select statement in my final
select statement.  So basically the following:

select x from table_a where name = @inputname

select date, name from table_a where level like x + '%'

where the x is from select statement 1

Is this possible?

Thanks,
Todd

6. Need help w/ SELECT statement within a SELECT statement

7. Having trouble

8. Kinda new and having trouble with SQL stored procedure



Return to MS SQL SERVER

 

Who is online

Users browsing this forum: No registered users and 67 guest