![]() | |
![]() |
| | Thread Tools | Display Modes |
#1
| |||
| |||
|
#2
| |||
| |||
|
|
Let's say I have 2 tables related: Owner: --------- o_id o_name Dog: --------- d_id d_name o_id - for Owner table. If the data is laid out as o_id o_name 1 John d_id d_name o_id 1 Skippy 1 2 Fido 1 How can I make a query that will produce the following results: o_id o_name owned dog names 1 John Skippy, Fido I think it has something to do with unions but I can't seem to get it. I'm using SQL Server Compact Edition. |
#3
| |||
| |||
|
|
I don't know if all this syntax is available, in Compact Edition, though. (While called SQL Server, it's an entirely different engine.) Else you will have run a cursor to achieve this result. |
#4
| |||
| |||
|
|
"Erland Sommarskog" <esquel (AT) sommarskog (DOT) se> wrote in message news:Xns98EDF177951DAYazorman (AT) 127 (DOT) 0.0.1... I don't know if all this syntax is available, in Compact Edition, though. (While called SQL Server, it's an entirely different engine.) Else you will have run a cursor to achieve this result. I had to work with the Compact Edition recently and it supports really a limited set of T-SQL. There are no stored procedures, user functions, control of flow constructs (like IF..ELSE, but CASE is available), views, triggers, and cursors. None of the new ranking functions for SQL 2005 are available as well as the XML specific handling (like FOR XML). Also, CROSS APPLY is not supported, and no DECLARE for variables... It is really "compact" and to keep it that way it has only the basic query support of the database engine (and I mean really basic - I could not even use derived tables in FROM and subqueries in the SELECT list). For all other functionality it depends on the rich functionality of ADO.NET and/or your application layer. So, probably the best way to approach this problem is to solve it in the application logic. HTH, Plamen Ratchev http://www.SQLStudio.com |
#5
| |||
| |||
|
|
I had to work with the Compact Edition recently and it supports really a limited set of T-SQL. There are no stored procedures, user functions, control of flow constructs (like IF..ELSE, but CASE is available), views, triggers, and cursors. None of the new ranking functions for SQL 2005 are available as well as the XML specific handling (like FOR XML). Also, CROSS APPLY is not supported, and no DECLARE for variables... |
#6
| |||
| |||
|
#7
| |||
| |||
|
|
I'm sure this has been brought up many times, but I will ask anyway. |
|
How can I make a query that will produce the following results: |
#8
| |||||
| |||||
|
|
But do you want to be a good SQL programmer instead? |
|
Yes it is brought up all the time because people will not bother to read even one book on RDBMS, so they keep asking this kind of thing over and over. Please post DDL, so that people do not have to guess what the keys, constraints, Declarative Referential Integrity, data types, etc. in your schema are. Let's start by doing what you should have done for us: |
|
Why do you wish to destroy First Normal Form (1NF) with a concatenated list structure? It is the foundation of RDBMS, after all. See why I say you never read a book on RDBMS. |
|
Why did you think that an owner is an attribute of a dog? It is a relationship! It might have attributes of its own, like license numbers, issue date, etc. but let's ignore that. Why are you formatting data in the back end? The basic principle of a tiered architecture is that display is done in the front end and never in the back end. This is a more basic programming principle than just SQL and RDBMS. |
|
I'm sure this has been brought up many times, but I will ask anyway. Yes it is brought up all the time because people will not bother to read even one book on RDBMS, so they keep asking this kind of thing over and over. Please post DDL, so that people do not have to guess what the keys, constraints, Declarative Referential Integrity, data types, etc. in your schema are. Let's start by doing what you should have done for us: CREATE TABLE Owners -- plural if you have more than one (owner_id INTEGER NOT NULL PRIMARY KEY, owner_name CHAR(20) NOT NULL); CREATE TABLE Dogs -- plural if you have more than one (dog_id INTEGER NOT NULL PRIMARY KEY, dog_name CHAR(20) NOT NULL); Why did you think that an owner is an attribute of a dog? It is a relationship! It might have attributes of its own, like license numbers, issue date, etc. but let's ignore that. CREATE TABLE Ownership-- plural if you have more than one (owner_id INTEGER NOT NULL REFERENCES Owners(owner_id) ON UPDATE CASCADE ON DELETE CASCADE, dog_id INTEGER NOT NULL REFERENCES Dogs(dog_id) ON UPDATE CASCADE ON DELETE CASCADE, PRIMARY KEY (dog_id, owner_id)); How can I make a query that will produce the following results: Why do you wish to destroy First Normal Form (1NF) with a concatenated list structure? It is the foundation of RDBMS, after all. See why I say you never read a book on RDBMS. Why are you formatting data in the back end? The basic principle of a tiered architecture is that display is done in the front end and never in the back end. This is a more basic programming principle than just SQL and RDBMS. Yes, trhre are some proprietary, stinking kludges that can do this. But do you want to be a good SQL programmer instead? |
#9
| |||
| |||
|
|
I'm sure this has been brought up many times, but I will ask anyway. Let's say I have 2 tables related: Owner: --------- o_id o_name Dog: --------- d_id d_name o_id - for Owner table. If the data is laid out as o_id o_name 1 John d_id d_name o_id 1 Skippy 1 2 Fido 1 How can I make a query that will produce the following results: o_id o_name owned dog names 1 John Skippy, Fido I think it has something to do with unions but I can't seem to get it. I'm using SQL Server Compact Edition. |
#10
| |||
| |||
|
|
On Mar 9, 3:43 am, "Steve London" <sylon... (AT) optonline (DOT) net> wrote: I'm sure this has been brought up many times, but I will ask anyway. Let's say I have 2 tables related: Owner: --------- o_id o_name Dog: --------- d_id d_name o_id - for Owner table. If the data is laid out as o_id o_name 1 John d_id d_name o_id 1 Skippy 1 2 Fido 1 How can I make a query that will produce the following results: o_id o_name owned dog names 1 John Skippy, Fido I think it has something to do with unions but I can't seem to get it. I'm using SQL Server Compact Edition. hi, Following code will work but you might have to customize it to fit your requirement. declare @table table (o_id int, o_name varchar(50)) declare @dog table (d_id int, d_name varchar(50),o_id int) declare @owner_dogs table(o_id int, o_name varchar(50), d_name varchar(50)) insert @table values (1,'John') insert @dog values(1,'Skippy',1) insert @dog values(2,'Fido',1) declare @dog_name varchar(50) while (select count(*) from @dog) > 0 begin set rowcount 1 if @dog_name is null begin select @dog_name = d_name from @dog where o_id = 1 end else select @dog_name = @dog_name + ', ' + d_name from @dog where o_id = 1 delete @dog end insert @owner_dogs values(1,'John',@dog_name) select * from @owner_dogs set rowcount 0- Hide quoted text - - Show quoted text - |
![]() |
| Thread Tools | |
| Display Modes | |
| |