I am trying to do a simple inner join to select a row or even just get ID from inner join of 2 tables, one is the regular ASP.NET users table and the other one is mine (interpreters) somehow my syntax is not accessible -
http://pastebin.com/4aDPrtst
I think it expect something like
[Common].[tbl_Interpreter_Account].[AspNetUsers]
with all the [][][][ but I am not sure
Assuming [Common] is your database schema and [AspNetUsers] is the table automatically created using membership services (or by some other means), I'm unsure what [tbl_Interpreter_Account] refers to. Could you please clarify?
Also the join needs to be corrected.
You're joining what appears to be the following two tables: AspNetUsers and AddIntrepeter. But the join condition is on some other table Interpreter_Account
Try the following:
Insert your databasename at the top of the procedure.
USE [Common]
CREATE PROCEDURE [dbo].[GetIntIdByEmail]
#email nvarchar(50)
AS
SELECT
AspNetUsers.UserName
FROM [AspNetUsers] users
INNER JOIN [<<Your_Table>>] interpreter ON users.Id = interpreter.<<your_Matching_Column>>
WHERE interpreter.contact_email = #email
GO
Related
I have a task where I have to identify the company database tables last_user_update and then store that in a separate table where reports for each tables last update can be monitored. What I'm aiming to do is have my query loop through a list of table names and so far I have the following:
Identify the all tables in the database:
INSERT INTO [CorpDB1].[dbo].[tblTableNames]
([name])
SELECT *
FROM sys.Tables
ORDER by name
The result is a table containing a 984 rows of table names for all tables in the DB.
Next I have the following:
Query to return and insert last_user_update into a new table called DatabaseTableHistory:
INSERT INTO [CorpDB1].[dbo].[DatabaseTableHistory]
([DatabaseName]
,[last_user_update]
,[database_id]
,[object_id]
,[index_id]
,[user_seeks]
,[user_scans]
,[user_lookups]
,[user_updates]
,[last_user_seek])
SELECT OBJECT_NAME(OBJECT_ID) AS DatabaseName, last_user_update,
database_id, object_id, index_id, user_seeks, user_scans, user_lookups,
user_updates, last_user_seek
FROM sys.dm_db_index_usage_stats
WHERE database_id = DB_ID( 'CorpDB1')
AND OBJECT_ID=OBJECT_ID('tblStatesList') AND last_user_update <
'20150430'
This query works as designed. What I'm trying to do is have the last query loop through the table containing the list of table names inserting it where OBJECT_ID=OBJECT_ID('tbleStatesList') is so I don't have to manually run the query by typing each table name in by hand. Any suggestions would be appreciated.
First things first, apart from being wrong (SELECT * will return much more than just name), your first query is completely unnecessary. There's no need to select your table names into a cache table.
You can get the list of tables you need by using an INNER JOIN between sys.dm_db_index_usage_stats and sys.tables on the object_id.
I'm assuming you want all the indexes on CorpDB1 tables that have a last_user_update prior to 30th April 2015.
USE CorpDB1;
GO
INSERT INTO [CorpDB1].[dbo].[DatabaseTableHistory]
([DatabaseName]
,[last_user_update]
,[database_id]
,[object_id]
,[index_id]
,[user_seeks]
,[user_scans]
,[user_lookups]
,[user_updates]
,[last_user_seek])
SELECT
DB_NAME(ddius.database_id) AS DatabaseName, -- Fix this on your query
ddius.last_user_update,
ddius.database_id,
ddius.object_id,
ddius.index_id,
ddius.user_seeks,
ddius.user_scans,
ddius.user_lookups,
ddius.user_updates,
ddius.last_user_seek
FROM sys.dm_db_index_usage_stats ddius
INNER JOIN sys.tables AS t
ON t.object_id = ddius.object_id
WHERE database_id = DB_ID( 'CorpDB1')
AND last_user_update < '20150430';
If you are working with MSSQL, I would suggest creating a stored procedure to loop through the first dataset using a cursor.
See the link below for a short tutorial on how to create and user cursors for this purpose.
http://stevestedman.com/2013/04/t-sql-a-simple-example-using-a-cursor/
I am writing a script to pull out data from existing stored procedures. What I want to do is pull out all connections from a single source.
I.E. we have a select in a stored procedure. That select is as follows,
Select data from dbo.table1 t1
inner join dbo.table2 t2 on t1.pk=t2.pk
inner join dbo.table3 t3 on t2.pk=t3.pk
I want to pull out dbo.table1, dbo.table2, and dbo.table3
Edit:
To clear up, from that select statement, I want to pull out dbo.table1, dbo.table2, and dbo.table3 into an output or insert it into a table. Basically, I'm trying to get a list of all tables from all stored procedures.
Also, sysdepends does not work for every stored procedure due to some of the stored procedures existing at a linked server.
You can use sys.dm_sql_referenced_entities for SQL Server 2014 or sp_depends for previous versions.
In my searches I have found many threads that deal with my issue in round about ways but I have not been able to find one answer that clearly defines the answer.
Yes I'm a noob to SQL Server.
Here is what I am trying to do And I need to know the best way to do it. For the purposes of this question I will keep it simple. Remember it is the functionality I want
I have table A
ClientName,
manager name,
Location
Table B
Manager name
Manager location
Table C
Random columns,
Client name,
Manager name
I want to
take the rows returned from SELECT [client name], [manager name] from table c
Insert those rows into table a with #clientname and #manager name parameters already defined.
Once the row is inserted it will make
tableA.location = Select [manager location]
from tableb
where #manager = tableb.[manager name]
These needs to happen in a batch for multiple rows in one call
This is point in time data so no lookups and no blanket update queries
Use functions?
Stored procedures?
Triggers may not be the answer since I will be inputting 500+ rows on each call
Thank you in advance
It is hard to believe you could not find something very closely related to your question. All you should have to do is
insert TableA
select C.ClientName, C.ManagerName, B.Location
from TableC C
join TableB B on B.ManagerName = C.ManagerName
where C.ManagerName = #ManagerName and C.ClientName = #clientname
Your stated usage case makes this look like you should wrap this inside of a stored proc. In fact, the statement above is the entire body of a stored proc
create proc sotest(#clientname varchar(40), #managername varchar(40)) as
begin
-- code above
end
It is not possible to avoid lookup of location from TableB as your have defined the problem (assuming you want to populated the Location column)
I'll start by saying hello! This forum has been a great help to me over the past few months, but have only now joined and asking my first question.
I'm working with the Northwind database in SQL Server 2008 r2 to build a vb.net application. I've been wrecking my head for a week trying to figure out how to make an order/invoice form. I can get the information I need to display using separate stored procs (GetCustInfo, GetOrderInfo, GetProductInfo, or something like that), but I'm having trouble figuring out how to display them on the form.
When I select all the info I need in one sp (as in the Invoice view which comes built in the db), I get 2155 rows, which is the number of items which have been ordered in the company history.
What I want to do is display this information, but navigate by OrderID (which would give me 830 rows, each with a certain number of products related to the OrderID). So I'm thinking I need different stored procs related which can be related in some way.
I'd really appreciate any help that can be given on this.
Many thanks in advance.
p.s. I have screenshots of the Northwind sample app which shipped/ships with Access, which is really what I'm trying to recreate in SQL Server. Unfortunately, no code!
MM
Yes you can achieve it by many ways and SP is one. Just create a SP to select that related products passing OrderId as a input parameter.
Some options (with contrived examples):
You can ALTER existing stored procedures to get what you want (not recommended if you want to use the existing procedures for other queries).
ALTER PROCEDURE usp_ExistingProcedure
AS
BEGIN
SELECT t1.Value
, t2.Value
-- Supose that this was the addition we made to an existing stored procedure
, t2.ValueTwo
FROM TableOne t1
INNER JOIN TableTwo t2 ON t1.ID = t2.ID
END
You can CREATE new stored procedures for your queries; in the above example, it would be a create procedure with a new name.
You may be able to create a VIEW to obtain what you need - this will operate a little differently.
CREATE VIEW uv_ApplicationView
AS
SELECT t1.Value
, t2.Value
, t2.ValueTwo
FROM TableOne t1
INNER JOIN TableTwo t2 ON t1.ID = t2.ID
You can pull the query directly from the VB application, though if you want to reuse it for something else, I wouldn't recommend this approach.
// A re-usable approach calling a stored procedure
SqlCommand myQuery = new SqlCommand("EXECUTE usp_myQuery", sqlConn);
// A query directly in the C# code:
string msQuery = "SELECT t1.Value, t2.Value, t2.ValueTwo FROM TableOne t1 INNER JOIN TableTwo t2 ON t1.ID = t2.ID"
// Later ...
SqlCommand myQuery = new SqlCommand(msQuery, sqlConn);
I have a SQL Server 2005 database that is linked to an Oracle database. What I want to do is run a query to pull some ID numbers out of it, then find out which ones are in Oracle.
So I want to take the results of this query:
SELECT pidm
FROM sql_server_table
And do something like this to query the Oracle database (assuming that the results of the previous query are stored in #pidms):
OPENQUERY(oracledb,
'
SELECT pidm
FROM table
WHERE pidm IN (' +
#pidms + ')')
GO
But I'm having trouble thinking of a good way to do this. I suppose that I could do an inner join of queries similar to these two. Unfortunately, there are a lot of records to pull within a limited timeframe so I don't think that will be a very performant option to choose.
Any suggestions? I'd ideally like to do this with as little Dynamic SQL as possible.
Ahhhh, pidms. Brings back bad memories! :)
You could do the join, but you would do it like this:
select sql.pidm,sql.field2 from sqltable as sql
inner join
(select pidm,field2 from oracledb..schema.table) as orcl
on
sql.pidm = orcl.pidm
I'm not sure if you could write a PL/SQL procedure that would take a table variable from sql...but maybe.....no, I doubt it.
Store openquery results in a temp table, then do an inner join between the SQL table and the temp table.
I don't think you can do a join since OPENQUERY requires a pure string (as you wrote above).
BG: Actually JOIN IN SQLServer to Oracle by OpenQuery works, avoiding #tmp table and allowing JOIN to SQL without Param* - ex.
[SQL SP] LEFT JOIN OPENQUERY(ORADB,
'SELECT COUNT(distinct O.ORD_NUM) LCNT,
O.ORD_MAIN_NUM
FROM CUSTOMER.CUST_FILE C
JOIN CUSTOMER.ORDER_NEW O
ON C.ID = O.ORD_ID
WHERE C.CUS_ID NOT IN (''2'',''3'')
GROUP BY O.ORD_MAIN_MACNUM') LC
ON T.ID = LC.ORD_MAIN_ID*
Cheers, Bill Gibbs