Select distinct from single column in lightswitch - sql

I'm trying to execute this SQL query inside lightswitch:
Select distinct [columnName] from [table]
The query window only has Filter, Sort, and Parameters
I have arrived on the DataService.cs file where I can manualy edit the queries, but im stuck here:
partial void QueryName_PreprocessQuery(ref IQueryable<ServiceUser> query)
{
query = query.Distinct();
}
This selects the whole table. I want the distinct values of only one column.
Note that I can add a view to the database and update it as an entity but thats not what I'm looking for..

Related

Get the list of result from first sql query and execute second query on the result of list of first query

So, I am working with redshift and SQL for the first time. I have run into this problem due to my limted knowledge of SQL.
I have this first query which return me tables with the columnA. (TableX, TableY... etc)
SELECT tablename
FROM PG_TABLE_DEF
WHERE ("column" = 'columnA');
Also I have this second query which returns me all the rows from the table containig certain value of columnA.
SELECT *
FROM TableX
WHERE columnA='123934'
What I want to achieve is take the result from the first query which is list of tables, and for each table run the second query i.e. get the rows with value of columnA=123934 for each table returned from first query.
What you want to achieve is done using dynamic SQL. Dynamic queries let you run a query from a string.
I am not an Redshit user but to generate the SQL string you need to run you could use the following query:
SELECT 'SELECT * FROM '||tablename ||' WHERE ColumnA= ''123934''; '
FROM PG_TABLE_DEF
WHERE ("column" = 'columnA');
You can try running it manually.

Read XML string in SQL and Dynamically select value from XMl string

I have One table "tblXMLData" with One column "coreData".
I am inserting XML string in this table.
I want to read this xml and insert record into another table.
When i write bellow code it is working fine,
Declare #xmlRec xml
Select TOP 1 #xmlRec = coreData FROM tblXMLData
SELECT
tmpTable.col.query ('FName').value('.','varchar(50)') AS FName,
tmpTable.col.query ('LName').value('.','varchar(50)') AS LName,
tmpTable.col.query ('Mobile').value('.','varchar(50)') AS Mobile
FROM #xmlRec.nodes('data/data') AS tmpTable(col)
But I want to create select statement Dynamically. Like I am read column name from another table and creating dynamic select statement.
When i execute that statement it will not work because "#xmlRec.nodes" is giving error.
I want to create dynamic Query and insert selected record into another table.
Please help....

SQL: how to find a mapping of view columns to table columns?

In Microsoft SQL Server 2008 R2, let's say my database has the following view:
create view [dbo].[MyView]
(
[MyColumnA]
)
AS
(SELECT MyColumnB FROM MyTable)
Now let's suppose I only know that there is a view called MyView that has a column called MyColumnA, but I don't know that it maps to MyTable.ColumnB. What is the easiest/fastest way to determine which table and column MyView.ColumnA maps to? Is there a query that can tell me this? Something like:
SELECT TABLE_NAME, TABLE_COLUMN_NAME
FROM INFORMATION_SCHEMA.VIEW_MAPPINGS
WHERE VIEW_NAME = 'MyView' AND VIEW_COLUMN_NAME = 'MyColumnA'
This query would return [MyTable, MyColumnB].
Currently I have to find the view in SSMS Object Explorer, right click it and generate the create script, then search for the name of the view's column. Then I note which ordinal position it is in the view (let's say 4th column), and have to find the corresponding 4th column in the select statement. The select statement will most likely be using a table alias, so then I have to look through the JOIN statements to find the table name based on the alias.
This is quite time consuming, and I'm hoping to find a faster way, if not by a query then perhaps by some other process that is faster or easier than mine.
SP_DEPENDS should work
SP_DEPENDS 'MyView'

Inserting a new column into SQL

I have these queries:
SELECT *
FROM dbo.GRAUD_ProjectsByCostCategory
select right(CostCategoryId,14) as CostBreak
from dbo.GRAUD_ProjectsByCostCategory
They work well in that they give me the correct data, but I would like to know how to combine the new column CostBreak into the table of results rather than as a separate query result.
An example of the results I get are as below:
Where I want them in the same table
The data is coming from the same table so you should be able to just add that value to your initial query. You do not even have to perform a join to get it:
SELECT name,
description,
project,
CostCategoryId,
right(CostCategoryId,14) as CostBreak
FROM dbo.GRAUD_ProjectsByCostCategory

Create an insert script from select results

Using SQL Server Management Studio is there a way I can select one or more rows in the grid of select results and have SQL Server Mangement Studio generate one or more insert statements (one for each row selected) which would insert that data into a table with the same schema?
Edit: I know how to create one manually, but I was hoping there would be something that would create it automatically for me. If you are familiar with Toad there is a way to have Toad generate inserts based on data in the results pane and I was hoping SSMS had an equivalant function.
Try to save the query result into a disposable table.
For example:
SELECT * INTO disposable_customer_table FROM customer_table WHERE id IN (in range of something)
Then do a db -> Tasks -> Generate Scripts.
Select specific database objects.
Choose disposable_customer_table from the list of table names.
Choose Save to file.
Make sure to do an Advance setup and select "Data only" from the 'Types of data to script'.
Tweak the result file and rename the disposable_customer_table back to the original table name.
Clean it up and drop the disposable_customer_table.
select 'insert into tableB values (', tableA.x ,',',tableA.y,',',tableA.z,')' from tableA
I think you have two options here:
Create your inserts manually. For instance:
select Name, Surname,
'insert into Person (Name,surname) values ('''+Name+''','''+Surname+')'
from Person
This gets you the results and, in the last column, the insert script for the row. You can then select and paste it in an Editor window.
Right click on the db -> Tasks -> Generate Scripts. Press then Advance and select "Data Only" (Default is Schema Only).
Perform your query and right click on the blank area where the column headers meet the row number in the Results view.
You can then select Script Grid Results: