Showing posts with label somebody. Show all posts
Showing posts with label somebody. Show all posts

Saturday, February 25, 2012

A question from a newbie

Can somebody tell me the correct syntax if I want to grant SELECT privileges
on all tables in a db?
I tried:
GRANT SELECT TO public;
Thanks.
VenkatAdd the user to the db_datareader fixed database role.
Geoff N. Hiten
Microsoft SQL Server MVP
Senior Database Administrator
Careerbuilder.com
I support the Professional Association for SQL Server
www.sqlpass.org
"vvenk" <vvenk@.discussions.microsoft.com> wrote in message
news:A63A543C-1176-4AEF-BC2E-2FBA35D5DB63@.microsoft.com...
> Can somebody tell me the correct syntax if I want to grant SELECT
privileges
> on all tables in a db?
> I tried:
> GRANT SELECT TO public;
> Thanks.
> Venkat|||Geoff:
Thanks a lot. I was reading some of the postings on this forum that
suggested that I have to write a script to cycle through all the tables.
Your's is the most elegant solution.
Venkat
"Geoff N. Hiten" wrote:

> Add the user to the db_datareader fixed database role.
> --
> Geoff N. Hiten
> Microsoft SQL Server MVP
> Senior Database Administrator
> Careerbuilder.com
> I support the Professional Association for SQL Server
> www.sqlpass.org
> "vvenk" <vvenk@.discussions.microsoft.com> wrote in message
> news:A63A543C-1176-4AEF-BC2E-2FBA35D5DB63@.microsoft.com...
> privileges
>
>|||To add to Geoff's response, the built-in db_datareader and db_datawriter
roles are fine if these meet your security requirements. Granting
permissions to individual users or user-defined roles is appropriate when
you need more granular security and/or stored procedure execute permissions.
Utility scripts can be faster than granting permissions via Enterprise
Manager when you have a lot of objects.
Hope this helps.
Dan Guzman
SQL Server MVP
"vvenk" <vvenk@.discussions.microsoft.com> wrote in message
news:2FB6F0F8-D8B8-47B0-8743-DD51C39D4A5E@.microsoft.com...[vbcol=seagreen]
> Geoff:
> Thanks a lot. I was reading some of the postings on this forum that
> suggested that I have to write a script to cycle through all the tables.
> Your's is the most elegant solution.
> Venkat
> "Geoff N. Hiten" wrote:
>

Friday, February 24, 2012

A question about db relationships

I am starting a new project and somebody told me at work that if I create relationships (foreign keys) between my tables, it makes the database slower, the best way is to "link" the tables using sql instructions like inner/outer join.
Is that true?
Thanks in advance,
venusgirlwhat moron told you that?|||Many developers are leary of Foreign Key relationships becase it "takes away" some of the control they have in the app area. During the development phase of a new project, it can also be a pain to clear out data in a table that has foreign keys enabled.

I am not aware of any (significant) performance penalty that foreign keys impose on a database. They are of tremendous value long term in maintaining relational integrity and for a host of other reasons.

In the end, Brett's assessment is...er...rather more to the point, but spot on.

Regards,

hmscott|||So I see that there's no performance problems in creating my database with foreign keys. :) Thanks guys.
One more question, that could be applied to any database or only to SQL Server?

Thanks,
venusgirl|||There is always some perofrmance cost associated with foreign keys, because the database needs to do a lookup to ensure that the parent row exists. That cost is normally trivial, and compared to the human cost of doing without foreign keys it is always trivial.

Some databases impose a higher "performance price" than others for implementing foreign keys, but I've never seen one where that cost was material. Compared to the hours of maintenance time and the uncertainty of the users when a system runs without foreign key definitions, I see them as cheap.

-PatP|||"Data Sanitation" after the horse has left the barn is a very time consuming thing.

I like the fact that I don't have to believe or rely on developers who say they will "take care of everything"

They always lie

DB2/Oracle/SQL Server and most database management systems take care of RI.

I wonder about MySQL though... I really should play with that...which is what this board is based on I believe|||All current (post 4.0) flavors of MySQL can declare RI (referential integrity). At least one of the flavors of MySQL (InnoDb) can actually enforce RI. Very few of the MySQL implementations that I've seen use a file engine that can actually enforce RI even though they can declare it, which makes me really uncomfortable.

-PatP|||What's the point of declaring RI if it's not enforced?|||What's the point of declaring RI if it's not enforced?It makes lovely documentation?

-PatP|||Really, this thread could have stopped with Brett's first post.

But...MySql can't enforce relationial integrity? I had no idea. In my opinion a database without primary or foreign keys is not a database. It is a datapile.|||But...MySql can't enforce relationial integrity? I had no idea. In my opinion a database without primary or foreign keys is not a database. It is a datapile.Not exactly... MySQL actually supports mutliple (at least three) file formats for holding data. One of those formats (InnoDb) allows you to support DRI.

The "Front End" for MySQL always supports DRI syntax (at least since version 4.0), whether or not the currently selected back end will enforce the DRI or not.

I've never seen an ISP host InnoDB, but that doesn't mean that none of them host it. InnoDb is considerably more complex to manage, and resource intensive than MyISAM. Because of this choice that is commonly made by implementers in the name of speed and ease of maintenance, MySQL gets a bad name.

-PatP

A quest for a query

Hello all!
Could somebody please help me with the following query: I have a table with
3 colums. First column are just identity numbers. Second column contains
data in the following pattern: 2 rows of data, 1 null (empty) row and then
again 2 rows on data, 1 null row, etc. Third column is as now empty.
Now, the query should take each second "data" row of the second column
(cells in rows 2, 5, 8, 11,14, etc.) and copy their contents to the third
column but one row higher (column 2, row 2 should be copied to column 3,
row, c2 r5 to c3 r4, etc.) in a sort of partial transposing of the
table...I don't have an idea how to accomplish that.
Thank you in advance!
HrvojeTry,
update
a
set
a.colC = b.colB
from
t as a
inner join
t as b
on a.colA = b.colB - 1
and (b.colB = 2 or b.colB % 3 = 2)
AMB
"Hrvoje Vrbanc" wrote:

> Hello all!
> Could somebody please help me with the following query: I have a table wit
h
> 3 colums. First column are just identity numbers. Second column contains
> data in the following pattern: 2 rows of data, 1 null (empty) row and then
> again 2 rows on data, 1 null row, etc. Third column is as now empty.
> Now, the query should take each second "data" row of the second column
> (cells in rows 2, 5, 8, 11,14, etc.) and copy their contents to the third
> column but one row higher (column 2, row 2 should be copied to column 3,
> row, c2 r5 to c3 r4, etc.) in a sort of partial transposing of the
> table...I don't have an idea how to accomplish that.
> Thank you in advance!
> Hrvoje
>
>|||How does the data get populated in this table? The problem is that you
can't normally control the order in which IDENTITY values are allocated
so when you say "each second row" or Nth row or whatever, there may be
no guarantee that the rows numbered 1,2 or 3 are what you expected them
to be. Do not rely on the IDENTITY values being sequential unless you
populate all the data using the SET IDENTITY_INSERT option (in which
case, why use IDENTITY at all?).
The second issue is with the principle of this table design. Leaving
aside the difficulty with IDENTITY, my impression is that this is not a
correctly normalized table. I'll assume then that you intend to create
a new table and that the purpose of your query is to transform the data
into the correct format.
With that assumption in mind it's difficult to make much sense of your
final paragraph. What does "one row higher" mean? Rows should be
identifiable from keys not from row "position numbers".
In short, we'll need some more information to help you out: DDL, sample
data INSERTs and required end result. Please do include keys and
constraints if you post DDL. See this article for info on how to do
this:
http://www.aspfaq.com/etiquette.asp?id=5006
David Portas
SQL Server MVP
--|||Hello!
It's a static flat table, no further population and no changes except the
one I'm searching the query for.
One row higher means the following: every row has the primary key that is an
ordinal number. I want to copy the data from one colum to another but to the
row with lower ordinal number.
Hrvoje
"David Portas" <REMOVE_BEFORE_REPLYING_dportas@.acm.org> wrote in message
news:1110205802.879690.42060@.o13g2000cwo.googlegroups.com...
> How does the data get populated in this table? The problem is that you
> can't normally control the order in which IDENTITY values are allocated
> so when you say "each second row" or Nth row or whatever, there may be
> no guarantee that the rows numbered 1,2 or 3 are what you expected them
> to be. Do not rely on the IDENTITY values being sequential unless you
> populate all the data using the SET IDENTITY_INSERT option (in which
> case, why use IDENTITY at all?).
> The second issue is with the principle of this table design. Leaving
> aside the difficulty with IDENTITY, my impression is that this is not a
> correctly normalized table. I'll assume then that you intend to create
> a new table and that the purpose of your query is to transform the data
> into the correct format.
> With that assumption in mind it's difficult to make much sense of your
> final paragraph. What does "one row higher" mean? Rows should be
> identifiable from keys not from row "position numbers".
> In short, we'll need some more information to help you out: DDL, sample
> data INSERTs and required end result. Please do include keys and
> constraints if you post DDL. See this article for info on how to do
> this:
> http://www.aspfaq.com/etiquette.asp?id=5006
> --
> David Portas
> SQL Server MVP
> --
>|||Thanks!
I'll try!
Hrvoje
"Alejandro Mesa" <AlejandroMesa@.discussions.microsoft.com> wrote in message
news:5F3F369B-96E8-4611-AF48-75817E7B0399@.microsoft.com...
> Try,
> update
> a
> set
> a.colC = b.colB
> from
> t as a
> inner join
> t as b
> on a.colA = b.colB - 1
> and (b.colB = 2 or b.colB % 3 = 2)
>
> AMB
>
> "Hrvoje Vrbanc" wrote:
>

Sunday, February 19, 2012

A query I''ve encounter that has me stumped ... anybody?


Hello Everyone,

I was hoping somebody could help me out with a query I've been trying to solve.

My Table Structure:

[UserMessages]

MessageID int
ToUserID int
FromUserID int
Subject varchar(200)
Message varchar(max)
isNew bit

[UserMessageReplies]

ReplyMessageID int
MessageID int
ToUserID int
FromUserID int
Message varchar(max)
isNew bit

Sample Data:

[UserMessages]

MessageID | ToUserID | FromUserID | Subject | Message | isNew
-
1 1 2 test subject Message Body 0
2 2 1 test subject Message Body 0
3 1 4 test subject Message Body 1
4 1 5 test subject Message Body 1

[UserMessageReplies]

ReplyMessageID | MessageID | ToUserID | FromUserID | Message | isNew

1 1 2 1 re: Message Body 0
2 1 1 2 re: Message Body 0
3 1 2 1 re: Message Body 0
4 1 1 2 re: Message Body 0
5 1 2 1 re: Message Body 1
6 2 1 2 re: Message Body 1

Explanation:

ReplyMessageID = 1-4 signfies that there is a thread response to MessageID 1 but they have been opened (isNew =0).
ReplyMessageID = 5 signfies that there is a new thread response to MessageID 1.
ReplyMessageID = 6 signifies that there is a reply from a message UserID=1 sent out (MessageID 2)

There needs to be a filter to check UserMessages.TOUserID=1 OR UserMessageReplies.ToUserID=1 to ensure that we can capture a msg that UserID=1 had sent out but now there is a reply to the Message by the User (in this example data i've setup MessageID = 2 to handle that possibility)

My Goal:

To select all messages for a ToUserID and order them by UserMessages.isNew or UserMessageReplies.isNew. This must include a message the user has sent but now has received a reply. (This means the first filter; UserMessages.ToUserID, must be overwritten with UserMessageReplies.ToUserID)

Thanks very much!

Chris

I can't quite follow what you want. Could you do a few additional things:

1. Turn your table structures into DDL and DML, so test SQL Statements can be written

2. Put out the results you want to see exactly. That would be a big help.

Thanks

|||CREATE TABLE [dbo].[UserMessages](
[MessageID] [int] IDENTITY(1,1) NOT NULL,
[ToUserID] [int] NOT NULL,
[FromUserID] [int] NOT NULL,
[Subject] [varchar](200) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL,
[Message] [varchar](max) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL,
[isNew] [bit] NOT NULL CONSTRAINT [DF_UserMessages_isNew] DEFAULT ((1)),
[DateSent] [datetime] NOT NULL CONSTRAINT [DF_UserMessages_DateSent] DEFAULT (getdate()),
CONSTRAINT [PK_UserMessages] PRIMARY KEY CLUSTERED
(
[MessageID] ASC
)WITH (PAD_INDEX = OFF, IGNORE_DUP_KEY = OFF) ON [PRIMARY]
) ON [PRIMARY]

CREATE TABLE [dbo].[UserMessagesReplies](
[ReplyMessageID] [int] IDENTITY(1,1) NOT NULL,
[MessageID] [int] NOT NULL,
[ToUserID] [int] NOT NULL,
[FromUserID] [int] NOT NULL,
[Message] [varchar](max) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL,
[isNew] [bit] NOT NULL CONSTRAINT [DF_UserMessagesReplies_isNew] DEFAULT ((1)),
[DateSent] [datetime] NULL CONSTRAINT [DF_UserMessagesReplies_DateSent] DEFAULT (getdate()),
CONSTRAINT [PK_UserMessagesReplies] PRIMARY KEY CLUSTERED
(
[ReplyMessageID] ASC
)WITH (PAD_INDEX = OFF, IGNORE_DUP_KEY = OFF) ON [PRIMARY]
) ON [PRIMARY]

Result Set:

MessageID | ToUserID | FromUserID
-
1 1 2
2 2 1
3 1 4
4 1 5

MessageID=2 should appear because there is a response (ReplyMessageID=6) ToUserID=1

Select From UserMessages Where ToUserID=1

Thank you!
|||

It looks like you need to union the first table with the second table:

create view messagesTogether

as

select messageId, toUserId, fromUserId

from userMessages

union

select replyMessageId, toUserId, fromUserId

from userMessages

then get it where the toUserId is the same

But MessageId and ReplyMessageId don't match. I am uncomfortable with these table structures, so this is probably my best current guess. Am I close, does this make sense?

A query I''ve encounter that has me stumped ... anybody?


Hello Everyone,

I was hoping somebody could help me out with a query I've been trying to solve.

My Table Structure:

[UserMessages]

MessageID int
ToUserID int
FromUserID int
Subject varchar(200)
Message varchar(max)
isNew bit

[UserMessageReplies]

ReplyMessageID int
MessageID int
ToUserID int
FromUserID int
Message varchar(max)
isNew bit

Sample Data:

[UserMessages]

MessageID | ToUserID | FromUserID | Subject | Message | isNew
-
1 1 2 test subject Message Body 0
2 2 1 test subject Message Body 0
3 1 4 test subject Message Body 1
4 1 5 test subject Message Body 1

[UserMessageReplies]

ReplyMessageID | MessageID | ToUserID | FromUserID | Message | isNew

1 1 2 1 re: Message Body 0
2 1 1 2 re: Message Body 0
3 1 2 1 re: Message Body 0
4 1 1 2 re: Message Body 0
5 1 2 1 re: Message Body 1
6 2 1 2 re: Message Body 1

Explanation:

ReplyMessageID = 1-4 signfies that there is a thread response to MessageID 1 but they have been opened (isNew =0).
ReplyMessageID = 5 signfies that there is a new thread response to MessageID 1.
ReplyMessageID = 6 signifies that there is a reply from a message UserID=1 sent out (MessageID 2)

There needs to be a filter to check UserMessages.TOUserID=1 OR UserMessageReplies.ToUserID=1 to ensure that we can capture a msg that UserID=1 had sent out but now there is a reply to the Message by the User (in this example data i've setup MessageID = 2 to handle that possibility)

My Goal:

To select all messages for a ToUserID and order them by UserMessages.isNew or UserMessageReplies.isNew. This must include a message the user has sent but now has received a reply. (This means the first filter; UserMessages.ToUserID, must be overwritten with UserMessageReplies.ToUserID)

Thanks very much!

Chris

I can't quite follow what you want. Could you do a few additional things:

1. Turn your table structures into DDL and DML, so test SQL Statements can be written

2. Put out the results you want to see exactly. That would be a big help.

Thanks

|||CREATE TABLE [dbo].[UserMessages](
[MessageID] [int] IDENTITY(1,1) NOT NULL,
[ToUserID] [int] NOT NULL,
[FromUserID] [int] NOT NULL,
[Subject] [varchar](200) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL,
[Message] [varchar](max) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL,
[isNew] [bit] NOT NULL CONSTRAINT [DF_UserMessages_isNew] DEFAULT ((1)),
[DateSent] [datetime] NOT NULL CONSTRAINT [DF_UserMessages_DateSent] DEFAULT (getdate()),
CONSTRAINT [PK_UserMessages] PRIMARY KEY CLUSTERED
(
[MessageID] ASC
)WITH (PAD_INDEX = OFF, IGNORE_DUP_KEY = OFF) ON [PRIMARY]
) ON [PRIMARY]

CREATE TABLE [dbo].[UserMessagesReplies](
[ReplyMessageID] [int] IDENTITY(1,1) NOT NULL,
[MessageID] [int] NOT NULL,
[ToUserID] [int] NOT NULL,
[FromUserID] [int] NOT NULL,
[Message] [varchar](max) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL,
[isNew] [bit] NOT NULL CONSTRAINT [DF_UserMessagesReplies_isNew] DEFAULT ((1)),
[DateSent] [datetime] NULL CONSTRAINT [DF_UserMessagesReplies_DateSent] DEFAULT (getdate()),
CONSTRAINT [PK_UserMessagesReplies] PRIMARY KEY CLUSTERED
(
[ReplyMessageID] ASC
)WITH (PAD_INDEX = OFF, IGNORE_DUP_KEY = OFF) ON [PRIMARY]
) ON [PRIMARY]

Result Set:

MessageID | ToUserID | FromUserID
-
1 1 2
2 2 1
3 1 4
4 1 5

MessageID=2 should appear because there is a response (ReplyMessageID=6) ToUserID=1

Select From UserMessages Where ToUserID=1

Thank you!
|||

It looks like you need to union the first table with the second table:

create view messagesTogether

as

select messageId, toUserId, fromUserId

from userMessages

union

select replyMessageId, toUserId, fromUserId

from userMessages

then get it where the toUserId is the same

But MessageId and ReplyMessageId don't match. I am uncomfortable with these table structures, so this is probably my best current guess. Am I close, does this make sense?

A query I''ve encounter that has me stumped ... anybody?


Hello Everyone,

I was hoping somebody could help me out with a query I've been trying to solve.

My Table Structure:

[UserMessages]

MessageID int
ToUserID int
FromUserID int
Subject varchar(200)
Message varchar(max)
isNew bit

[UserMessageReplies]

ReplyMessageID int
MessageID int
ToUserID int
FromUserID int
Message varchar(max)
isNew bit

Sample Data:

[UserMessages]

MessageID | ToUserID | FromUserID | Subject | Message | isNew
-
1 1 2 test subject Message Body 0
2 2 1 test subject Message Body 0
3 1 4 test subject Message Body 1
4 1 5 test subject Message Body 1

[UserMessageReplies]

ReplyMessageID | MessageID | ToUserID | FromUserID | Message | isNew

1 1 2 1 re: Message Body 0
2 1 1 2 re: Message Body 0
3 1 2 1 re: Message Body 0
4 1 1 2 re: Message Body 0
5 1 2 1 re: Message Body 1
6 2 1 2 re: Message Body 1

Explanation:

ReplyMessageID = 1-4 signfies that there is a thread response to MessageID 1 but they have been opened (isNew =0).
ReplyMessageID = 5 signfies that there is a new thread response to MessageID 1.
ReplyMessageID = 6 signifies that there is a reply from a message UserID=1 sent out (MessageID 2)

There needs to be a filter to check UserMessages.TOUserID=1 OR UserMessageReplies.ToUserID=1 to ensure that we can capture a msg that UserID=1 had sent out but now there is a reply to the Message by the User (in this example data i've setup MessageID = 2 to handle that possibility)

My Goal:

To select all messages for a ToUserID and order them by UserMessages.isNew or UserMessageReplies.isNew. This must include a message the user has sent but now has received a reply. (This means the first filter; UserMessages.ToUserID, must be overwritten with UserMessageReplies.ToUserID)

Thanks very much!

Chris

I can't quite follow what you want. Could you do a few additional things:

1. Turn your table structures into DDL and DML, so test SQL Statements can be written

2. Put out the results you want to see exactly. That would be a big help.

Thanks

|||CREATE TABLE [dbo].[UserMessages](
[MessageID] [int] IDENTITY(1,1) NOT NULL,
[ToUserID] [int] NOT NULL,
[FromUserID] [int] NOT NULL,
[Subject] [varchar](200) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL,
[Message] [varchar](max) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL,
[isNew] [bit] NOT NULL CONSTRAINT [DF_UserMessages_isNew] DEFAULT ((1)),
[DateSent] [datetime] NOT NULL CONSTRAINT [DF_UserMessages_DateSent] DEFAULT (getdate()),
CONSTRAINT [PK_UserMessages] PRIMARY KEY CLUSTERED
(
[MessageID] ASC
)WITH (PAD_INDEX = OFF, IGNORE_DUP_KEY = OFF) ON [PRIMARY]
) ON [PRIMARY]

CREATE TABLE [dbo].[UserMessagesReplies](
[ReplyMessageID] [int] IDENTITY(1,1) NOT NULL,
[MessageID] [int] NOT NULL,
[ToUserID] [int] NOT NULL,
[FromUserID] [int] NOT NULL,
[Message] [varchar](max) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL,
[isNew] [bit] NOT NULL CONSTRAINT [DF_UserMessagesReplies_isNew] DEFAULT ((1)),
[DateSent] [datetime] NULL CONSTRAINT [DF_UserMessagesReplies_DateSent] DEFAULT (getdate()),
CONSTRAINT [PK_UserMessagesReplies] PRIMARY KEY CLUSTERED
(
[ReplyMessageID] ASC
)WITH (PAD_INDEX = OFF, IGNORE_DUP_KEY = OFF) ON [PRIMARY]
) ON [PRIMARY]

Result Set:

MessageID | ToUserID | FromUserID
-
1 1 2
2 2 1
3 1 4
4 1 5

MessageID=2 should appear because there is a response (ReplyMessageID=6) ToUserID=1

Select From UserMessages Where ToUserID=1

Thank you!
|||

It looks like you need to union the first table with the second table:

create view messagesTogether

as

select messageId, toUserId, fromUserId

from userMessages

union

select replyMessageId, toUserId, fromUserId

from userMessages

then get it where the toUserId is the same

But MessageId and ReplyMessageId don't match. I am uncomfortable with these table structures, so this is probably my best current guess. Am I close, does this make sense?

A query I''ve encounter that has me stumped ... anybody?


Hello Everyone,

I was hoping somebody could help me out with a query I've been trying to solve.

My Table Structure:

[UserMessages]

MessageID int
ToUserID int
FromUserID int
Subject varchar(200)
Message varchar(max)
isNew bit

[UserMessageReplies]

ReplyMessageID int
MessageID int
ToUserID int
FromUserID int
Message varchar(max)
isNew bit

Sample Data:

[UserMessages]

MessageID | ToUserID | FromUserID | Subject | Message | isNew
-
1 1 2 test subject Message Body 0
2 2 1 test subject Message Body 0
3 1 4 test subject Message Body 1
4 1 5 test subject Message Body 1

[UserMessageReplies]

ReplyMessageID | MessageID | ToUserID | FromUserID | Message | isNew

1 1 2 1 re: Message Body 0
2 1 1 2 re: Message Body 0
3 1 2 1 re: Message Body 0
4 1 1 2 re: Message Body 0
5 1 2 1 re: Message Body 1
6 2 1 2 re: Message Body 1

Explanation:

ReplyMessageID = 1-4 signfies that there is a thread response to MessageID 1 but they have been opened (isNew =0).
ReplyMessageID = 5 signfies that there is a new thread response to MessageID 1.
ReplyMessageID = 6 signifies that there is a reply from a message UserID=1 sent out (MessageID 2)

There needs to be a filter to check UserMessages.TOUserID=1 OR UserMessageReplies.ToUserID=1 to ensure that we can capture a msg that UserID=1 had sent out but now there is a reply to the Message by the User (in this example data i've setup MessageID = 2 to handle that possibility)

My Goal:

To select all messages for a ToUserID and order them by UserMessages.isNew or UserMessageReplies.isNew. This must include a message the user has sent but now has received a reply. (This means the first filter; UserMessages.ToUserID, must be overwritten with UserMessageReplies.ToUserID)

Thanks very much!

Chris

I can't quite follow what you want. Could you do a few additional things:

1. Turn your table structures into DDL and DML, so test SQL Statements can be written

2. Put out the results you want to see exactly. That would be a big help.

Thanks

|||CREATE TABLE [dbo].[UserMessages](
[MessageID] [int] IDENTITY(1,1) NOT NULL,
[ToUserID] [int] NOT NULL,
[FromUserID] [int] NOT NULL,
[Subject] [varchar](200) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL,
[Message] [varchar](max) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL,
[isNew] [bit] NOT NULL CONSTRAINT [DF_UserMessages_isNew] DEFAULT ((1)),
[DateSent] [datetime] NOT NULL CONSTRAINT [DF_UserMessages_DateSent] DEFAULT (getdate()),
CONSTRAINT [PK_UserMessages] PRIMARY KEY CLUSTERED
(
[MessageID] ASC
)WITH (PAD_INDEX = OFF, IGNORE_DUP_KEY = OFF) ON [PRIMARY]
) ON [PRIMARY]

CREATE TABLE [dbo].[UserMessagesReplies](
[ReplyMessageID] [int] IDENTITY(1,1) NOT NULL,
[MessageID] [int] NOT NULL,
[ToUserID] [int] NOT NULL,
[FromUserID] [int] NOT NULL,
[Message] [varchar](max) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL,
[isNew] [bit] NOT NULL CONSTRAINT [DF_UserMessagesReplies_isNew] DEFAULT ((1)),
[DateSent] [datetime] NULL CONSTRAINT [DF_UserMessagesReplies_DateSent] DEFAULT (getdate()),
CONSTRAINT [PK_UserMessagesReplies] PRIMARY KEY CLUSTERED
(
[ReplyMessageID] ASC
)WITH (PAD_INDEX = OFF, IGNORE_DUP_KEY = OFF) ON [PRIMARY]
) ON [PRIMARY]

Result Set:

MessageID | ToUserID | FromUserID
-
1 1 2
2 2 1
3 1 4
4 1 5

MessageID=2 should appear because there is a response (ReplyMessageID=6) ToUserID=1

Select From UserMessages Where ToUserID=1

Thank you!
|||

It looks like you need to union the first table with the second table:

create view messagesTogether

as

select messageId, toUserId, fromUserId

from userMessages

union

select replyMessageId, toUserId, fromUserId

from userMessages

then get it where the toUserId is the same

But MessageId and ReplyMessageId don't match. I am uncomfortable with these table structures, so this is probably my best current guess. Am I close, does this make sense?