This question already has answers here:
TSQL Comma Separation
(7 answers)
Closed 9 years ago.
I have three tables such as
User
+--+------+
|Id|Name |
+--+------+
|1 |Ram |
+--+------+
|2 |Rama |
+--+------+
|3 |Leesa |
+--+------+
|4 |Kelvin|
+--+------+
Role
+--+-------+
|Id|Name |
+--+-------+
|1 |Admin |
+--+-------+
|2 |FA |
+--+-------+
|3 |Testing|
+--+-------+
|4 |IT |
+--+-------+
User Role
+--+-------+-------+
|Id|User Id|Role Id|
+--+-------+-------+
|1 |1 |1 |
+--+-------+-------+
|2 |1 |2 |
+--+-------+-------+
|3 |2 |3 |
+--+-------+-------+
|4 |2 |1 |
+--+-------+-------+
|5 |3 |2 |
+--+-------+-------+
|6 |3 |3 |
+--+-------+-------+
|7 |4 |4 |
+--+-------+-------+
|8 |4 |2 |
+--+-------+-------+
From these 3 tables I want output like that
+--+---------+--------+
|Id|User Name|Roles |
+--+---------+--------+
|1 |Ram |Admin,FA|
+--+---------+--------+
You can do by creating a stored procedure.
CREATE PROC UserRoles
(
#UserId int
)
AS
begin
DECLARE #roles NVARCHAR(MAX) = ''
SELECT #roles = #roles + ',' + t.Roles
FROM (SELECT r.NAME AS Roles FROM [User Role] ur
Inner JOIN [ROLE] r ON r.Id = ur.RoleId
INNER JOIN User1 u ON u.Id = ur.UserID WHERE u.Id = #UserId) t
SET #roles = RIGHT(#roles, LEN(#roles)-1)
SELECT u.Id AS ID, u.NAME AS UserName, #roles AS Roles FROM USER1 u WHERE u.Id = #UserId
end
I would like to propose some function CONCATROWS for a future SQL standard.
SELECT page_id,CONCATROWS('\n',partext) AS pagetext FROM paragraph ORDER BY parposition GROUP BY page_id
I needed this function more-than-I-can-count times... not that I needed it badly, but it would have made life so much easier.
Related
I have this SQL Server database already stored
ID | TaxDecNo | OwnerName | PrevTaxDec
----------------------------------------------
1 | 5374 | John | 11135
2 | 9864 | Doe | 7394
3 | 11135 | John | 21784
4 | 7394 | Doe | 6872
5 | 21784 | John | NULL
6 | 6872 | Doe | NULL
When I'm going to display ID 3
ID | TaxDecNo | OwnerName | PrevTaxDec
----------------------------------------------
1 | 5374 | John | 11135
3 | 11135 | John | 21784
5 | 21784 | John | NULL
When I'm going to display RecordID 2
ID | TaxDecNo | OwnerName | PrevTaxDec
----------------------------------------------
2 | 9864 | Doe | 7394
4 | 7394 | Doe | 6872
6 | 6872 | Doe | NULL
The display only stop when no PrevTaxDec connects TaxDec No value.
I got this solution given to me a while but I don't know how to convert it to stored procedure which the data is already inserted the table
DECLARE #ID int = 3;
WITH YourTable AS(
SELECT V.ID,
V.TaxDecNo,
V.PrevTaxDec
FROM (VALUES(1,5374,11135), --This value must already inserted in the table
(2,9864,7394),
(3,11135,21784),
(4,7394,6872),
(5,21784,NULL), --I assume you aren't really mixing datatyoes. 'N/A' can't be inserted into an int column
(6,6872,NULL))V(ID,TaxDecNo,PrevTaxDec)),
--Solution
rCTEup AS(
SELECT YT.ID,
YT.TaxDecNo,
YT.PrevTaxDec
FROM YourTable YT
WHERE YT.ID = #ID
UNION ALL
SELECT YT.ID,
YT.TaxDecNo,
YT.PrevTaxDec
FROM rCTEup r
JOIN YourTable YT ON r.TaxDecNo = YT.PrevTaxDec),
rCTEdown AS(
SELECT YT.ID,
YT.TaxDecNo,
YT.PrevTaxDec
FROM YourTable YT
WHERE YT.ID = #ID
UNION ALL
SELECT YT.ID,
YT.TaxDecNo,
YT.PrevTaxDec
FROM rCTEdown r
JOIN YourTable YT ON r.PrevTaxDec = YT.TaxDecNo)
SELECT ID,
TaxDecNo,
PrevTaxDec
FROM rCTEup
UNION ALL
SELECT ID,
TaxDecNo,
PrevTaxDec
FROM rCTEdown
WHERE ID != #ID; --As it'll be in rCTEup
I'm currently writing a program in C# and I want to load friendships from the database based on the id passed as function parameter.
I got 2 tables (I only display the important columns).
Table 1: players
+----------------------+---------------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+----------------------+---------------------+------+-----+---------+----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| username | varchar(15) | NO | | NULL | |
+----------------------+---------------------+------+-----+---------+----------------+
Table 2: messenger_friends
+-------------+---------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------------+---------+------+-----+---------+-------+
| user_one_id | int(11) | NO | PRI | NULL | |
| user_two_id | int(11) | NO | PRI | NULL | |
+-------------+---------+------+-----+---------+-------+
The thing is, my idea was the following: in messenger_friends, save one line for a friendship. I know I can save 2 for one friendship but it would mean more storage as 500 friendships would become 1000 records. Now, in my application I have to JOIN messenger_friends to players. I got this function:
public async Task<IReadOnlyList<MessengerFriend>> GetFriends(int playerId)
In here, I need to get all records from messenger_friends where EITHER user_one_id OR user_two_id is playerId. Then in the same query, I want to join it to players. I know I can get the records this way:
SELECT * FROM messenger_friends WHERE user_one_id = {playerId} OR user_two_id = {playerId}
But I'm not sure how to join it to the players table as I need to join EITHER user_one_id OR user_two_id with players.id
SELECT *
FROM players p
JOIN messenger_friends f ON p.id in (f.user_one_id, f.user_two_id)
WHERE p.id = {playerId}
or
SELECT *
FROM players p
JOIN messenger_friends f ON p.id = f.user_one_id
OR P.id = f.user_two_id
WHERE p.id = {playerId}
I have two tables:
Table1 - MainTable
---------------------------------------
| MainTableID | CustomerName | BookID |
---------------------------------------
Table2 - BookTable
----------------------
| BookID | BookName |
----------------------
| 1 | physics |
----------------------
| 2 | Math |
----------------------
I want to get the result like this:
---------------------------------------
| MainTableID | CustomerName | BookID |
---------------------------------------
| 1 | Alex | Math |
---------------------------------------
I have list of BookNames in BookTable and I want to insert Data in MainTable. I am using ADO.NET entity data model in visual studio and I am doing this so:
BookTable correspondingBook=(from row in entities.BookTable
where rows.BookName == "Math"
select rows).First();
MainTable itemToAdd = new MainTable();
itemToAdd.CustomerName = "Alex";
itemToAdd.BookID = correspondingBook.BookID;
entities.MainTable.Add(itemToAdd);
entities.SaveChanges();
Is it good solution for this problem? if no, Which will be better?
Here is my enum values defined
public enum authaccess
{ read=0,create=1,update=2,delete=4}
As a Access Table looks like as below.
|id | tablename |columnname|permitted|
|----|-----------|----------|---------|
|1 | cms |header |3 |
|2 | cms |footer |2 |
|3 | cms |content |7 |
read access is permitted for all content, while 3 is for(create + update), and 7 for all rights.
As I need (x) button enabled in div where loggedInUser has delete authority
Using Linq, I used
new DbContext().tbl_access.where(k=>k.permitted > authaccess.delete){divid.class.add('close')}
but could not find a way to get a list of contents that can be changed by user ie 3 or (authaccess.create + authaccess.update)
[Clarification on edit]
The resultant table should be
|id | tablename |columnname|permitted|
|----|-----------|----------|---------|
|1 | cms |header |3 |
|2 | cms |footer |2 |
Because (header and footer) include create+update level access which is less than or equal to 3.
Thanks in advance for any help.
It looks like your authaccess has values for bit masking. For your requested create + update, a possible way to write your Linq is:
new DbContext().tbl_access
.Where(k => (k.permitted & (int)authaccess.create) > 0) &&
(k.permitted & (int)authaccess.update) > 0))
Do you want something like that;
var authAccessFilter = (int)authaccess.create + (int)authaccess.update;
var records = new DbContext().tbl_access.Where(k => k.permitted <= authAccessFilter).ToList();
I have 3 tables:
strings
-----------
- string_id
- fallback_text
translations
----------------------
- translation_id
- string_id
- locale_id
- text
locales
---------------------
- locale_id
I want to achieve a resultset like this:
string_id | locale_id | text
----------------------------
1 | en_US | bread
1 | es_ES | pan
1 | fr_FR | NULL
There is no translation for "bread" in french, but I want it in the results.
In SQL would be somethink like:
SELECT strings.string_id, locales.locale_id, translations.text
FROM strings
JOIN translations on strings.string_id = translations.string_id
RIGHT JOIN locales on translations.locale_id = locales.locale_id
This SELECT doesn't resolve my problem. What I want is to list every string who hasn't a translation in each locale existing in locales table. I think this isn't going to happen in a SELECT statement.
Imagine we have:
locales table:
|locale_id|
-----------
|en_US |
|es_ES |
strings table:
|string_id|fallback_text|
-------------------------
|1 |bread |
|2 |water |
translations table:
|translation_id|string_id|locale_id|translation
-----------------------------------------------
|1 |1 |en_US |bread
|2 |2 |es_ES |agua
I would like to achieve this resultset:
string_id | locale_id | text
----------------------------
1 | en_US | bread
1 | es_ES | NULL
2 | en_US | NULL
2 | es_ES | agua
Thanks in advance.
The easiest way to accomplish this is simply use a SQL view.
Create an SQL view with you select statement, then create an entity to match that view. This entity will have 3 properties string_id, locale_id, text. Make sure that this entity defines (string_id and locale_id) as primary key. If you are using code first your mappings would be something like:
ToTable("MyViewName");
HasKey(x => new { x.string_id, x.locale_id });
var query = from s in db.strings
from t in db.translation
where s.string_id == t.string_id
select new {s.stringid, t.locale_id, t.text }