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();
Related
I need to find all possible words using SOUNDEX.
For example:
I have the table (users) with one field (name):
Users have next view
|ID|Name |
|1 |Maria |
|2 |Ana Maria |
|3 |Maria Ana |
if I use Like, I have:
SELECT * FROM users WHERE name like '%Maria%'
As result I got
1.Maria
2.Ana Maria
3.Maria Ana
But if I use SOUNDEX
SELECT * FROM users WHERE SOUNDEX(name) = SOUNDEX('Maria')
I got the only two names (Maria and Maria Ana)
I need get result such as when I use like
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 }
I'm writing an interface to provide end-user input around a query.
An administrator defines the query, eg:
SELECT col1, col2, col3, col4 + col5 AS sumcol
FROM mytable
WHERE col10 = #parm1 AND somethingelse > #parm2
Now I can parse that query in C# getting all the returned column names
using (myReader = cmd.ExecuteReader(CommandBehavior.KeyInfo))
{
//Retrieve column schema into a DataTable.
schemaTable = myReader.GetSchemaTable();
//For each field in the table...
foreach (DataRow field in schemaTable.Rows)
{
// Display one-liner of important fields
Console.WriteLine("{0} - Base Table: [{1}] Data Type: [{2}]",
field[schemaTable.Columns["ColumnName"].ToString()].ToString(),
field[schemaTable.Columns["BaseTableName"].ToString()].ToString(),
field[schemaTable.Columns["DataType"].ToString()].ToString()
);
}
But I also want to get the data types that the parameters #parm1 and #parm2 are expecting. I can parse the string ok to get the parameter names, but I want to present to the end user input boxes for the 2 parameters and check they are the correct types before executing the query to get the results. There isn't necessarily a specific column name which I can check, eg is it was always colx = #parm1 then I could check the type of colx and assume #parm1 would be the same type, but the clause is likely to be often more complicated than that.
I don't have control over what query the admin creates, but I want to try to make it as fool proof as possible for the end user, prompting them to enter the correct type for the parameters, because I can't be sure the administrator is going to give sensible names to the parameters to indicate what the query expects. When I pass the query to SqlServer to get the return column types is there are way that it can also tell me what the parameter types might be?
I hope this makes sense.
If you are using SQL Server 2012 and above you can use sp_describe_undeclared_parameters stored procedure to guess parameter types.
For example, the following query executed on the Northwind sample database:
EXEC sp_describe_undeclared_parameters
N'SELECT * FROM Customers WHERE CustomerID = #CustomerID'
Gives you the following result:
+-------------------+-------------+--------------------------+----------------------------+----------------------+---------------------+-----------------+------------------------+------------------------------+----------------------------+--------------------------+----------------------------------------+-----------------------------+-----------------------------------+---------------------------------+-------------------------------+---------------------------+-----------------------------+------------------------------------+--------------------+---------------------+-----------------------+-----------------------+----------------------+
| parameter_ordinal | name | suggested_system_type_id | suggested_system_type_name | suggested_max_length | suggested_precision | suggested_scale | suggested_user_type_id | suggested_user_type_database | suggested_user_type_schema | suggested_user_type_name | suggested_assembly_qualified_type_name | suggested_xml_collection_id | suggested_xml_collection_database | suggested_xml_collection_schema | suggested_xml_collection_name | suggested_is_xml_document | suggested_is_case_sensitive | suggested_is_fixed_length_clr_type | suggested_is_input | suggested_is_output | formal_parameter_name | suggested_tds_type_id | suggested_tds_length |
+-------------------+-------------+--------------------------+----------------------------+----------------------+---------------------+-----------------+------------------------+------------------------------+----------------------------+--------------------------+----------------------------------------+-----------------------------+-----------------------------------+---------------------------------+-------------------------------+---------------------------+-----------------------------+------------------------------------+--------------------+---------------------+-----------------------+-----------------------+----------------------+
| 1 | #CustomerID | 239 | nchar(5) | 10 | 0 | 0 | NULL | NULL | NULL | NULL | NULL | NULL | NULL | NULL | NULL | 0 | 0 | 0 | 1 | 0 | NULL | 239 | 10 |
+-------------------+-------------+--------------------------+----------------------------+----------------------+---------------------+-----------------+------------------------+------------------------------+----------------------------+--------------------------+----------------------------------------+-----------------------------+-----------------------------------+---------------------------------+-------------------------------+---------------------------+-----------------------------+------------------------------------+--------------------+---------------------+-----------------------+-----------------------+----------------------+
I'm kind of new to this but I'll try to give as much detail as possible. This is my SQL table.
Customers
Customers_Id (PK) | First | Last | Address | Phone | Tech_Id (FK) |
-------------------+-------+-------+--------------+----------+--------------+
1 | Bob | Smith | 123 Fake St. | 3298492 | 1 |
2 | John | Man | 123 Noe St. | 2930482 | 1 |
3 | Tom | Lee | 123 Polk St. | 9308523 | 2 |
...
Tech
Tech_Id (PK) | First | Last | Phone | Customer_Count |
--------------+-------+-------+---------+----------------+
1 | Tim | Bo | 9384027 | |
2 | Andy | Wong | 9374927 | |
3 | Jack | Help | 2183847 | |
...
I'm trying to find the best way to count how many customer that each tech has either using SQL Query or C# coding. I was thinking of doing query with Count and then insert into the Customer_Count in Tech table.
I'm using visual studio 2012 and SQL is created locally in visual studio. Please help!
You can do it through SQL - using a GROUP BY clause to group the result by each tech. You can use the COUNT function to return the number of customers assigned to each tech. You can put this in an UPDATE statement to update the customer_count field in the tech table for each tech.
For example:
UPDATE t
SET t.customer_count = COUNT(c.customer_id)
FROM tech t
INNER JOIN customers c ON c.tech_id = t.tech_id
GROUP BY c.tech_id
Remove the Customer_Count column from the tech table. It's best not to make columns which duplicate available data unless there's a valid performance reason. If you want it displayed as such, then create a view:
SELECT t.tech_id,
t.first,
t.last,
t.phone,
(SELECT COUNT(c.customer_id)
FROM customers c
WHERE c.tech_id = t.tech_id) AS Customer_Count
FROM tech t
Now you can query it like a table, but you're not tasked with maintaining data which is always up to date through this view.
I prefre to using Sql ,but failed...Disappionted..
But I am sure that you can use Ado.net to do this job.
Here is C# demo code.
//query from db use C#
var allCus = new List<Customers>();
var gps = allCus.GroupBy(w => w.Tech_Id);
foreach(var gp in gps) {
var techId = gp.Key;
var cnt = gp.Count();
//update Tech set Customer_Count = cnt where Tech_Id = techId
}
Here is Sql version
update Tech set Customer_Count = (select IdAndCnt.cnt from (select Tech_Id,count (Tech_id) as cnt from Customers group by Tech_Id ) as IdAndCnt where Tech.Tech_Id = IdAndCnt.Tech_Id)
Holp it works.
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.