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);
Related
One of my clients has two Head Offices.
Each HO has almost 300 workstation and my client doesn't want to replicate all the info into one HO, so he asked if i can get some info into secondary tables with jobs from HO no. 2 to HO no. 1 for some reports...
This part is done and i had no issue but the real problem appeared when i had to modify the stored procedure for the reports.
Before i had the code like this:
SELECT D.IdDocument, D.etc...
FROM Document D (NOLOCK)
JOIN OtherTable OT (NOLOCK) on etc...
Now i've tried it like this:
1) First attempt:
SELECT DXD.IdDocument, DXD.etc...
FROM
(
SELECT D.*
FROM Document D (NOLOCK)
UNION ALL
SELECT D2.*
FROM RemoteDocument D2 (NOLOCK)
) DXD
JOIN OtherTable OT on etc...
2) Second attempt:
CREATE TABLE #TempDocument
(
IdDocument INT,
IdLocation INT,
etc..
)
CREATE INDEX IDX_TMP_Document ON #TempDocument (IdDocument, IdLocation)
INSERT INTO #TempDocument
SELECT DXD.*
FROM
(
SELECT D.*
FROM Document D (NOLOCK)
UNION ALL
SELECT D2.*
FROM RemoteDocument D2 (NOLOCK)
) DXD
SELECT DXD.IdDocument, DXD.etc...
FROM #TempDocument DXD
JOIN OtherTable OT (NOLOCK) on etc...
The problem is that before the sp ran in 5-10 minutes and now 30-40 minutes, the main issue that execution plan detected is in the union/insert using the union...
Can someone tell me a better/faster way to concatenate the info before using it?
I would recommend that you utilize a linked server. Then once you have created the linked server. Create a view to the linked server on one of your systems.
Server 1:
Linked Servers:
Server 2
Views:
MyNewView(Select * from openquery(linkedServer,'Select * from SomeDB.dbo.SomeTable')
When doing this. Your main server caches data from the view. So you can now work on one server and the return time should be fairly quick depending on the amount of data you're filtering through and returning.
for setting up a linked server please review the following article: Here.
This does not actually consolidate but it hopefully should improve some speed issues as well as allow you to write like you are in a single database. Cleaning up your code and also preventing any other communication problems as the only communication issue # the linked server end.
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.
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
In my form I have TADOQuery,TDataSetProvider,TClientDataSet,TDataSource,TDBGrid linked.
AdoQuery use SQL Server view to query data
AdoQuery.SQL:
Select * from vu_Name where fld=:fldval
Vu_Name:
SELECT * FROM t1 INNER JOIN t2 ON t2.fld1 = t2.fld1
in my dbgrid, only columns in table t1 are editable.(only t1 need to update )
What are the possible (fastest) ways to apply updates back to the server?
ClientDataSet.ApplyUpdates(0); // not working
Thank you.
TDataSetProvider has an event OnGetTableName where you should set the TableName parameter to t1. Thus the provider knows where to store the changed values.
You have to make sure that only fields of t1 are changed as TDataSetProvider will only update one table. Of course you can have different table names for different calls to ApplyUpdates. You can find out about changed fields in the DataSet parameter.
If you want to update more than one table you have to implement OnUpdateData, which gives you all of the freedom with all of the responsibility.
I have an inline query, in which I have one table1 in server1 and another table2 in server2.
I need to join these two tables, and fetch data.
I can do this like connect to one server, get data and connect to next server...fetch data.
and join them.
But is there any other better way. I have heard about Linked servers. Will that help here ?
Thanks in advance !!!
Yes, set up a linked server on one server to the other. Then you can just do a normal query with a join. It would look something like this:
SELECT t1.Col1
, t2.ColA
FROM server1Table t1
INNER JOIN SERVER2.dbname.dbo.tableName t2 ON t1.TheId = t2.TheId
this assumes you're running the query on Server1. You can also have two linked servers and reference them both using [servername].[dbname].[schema].[table] and then use in SQL as normal.
Alternatively, you can use OPENROWSET (but linked server is easiest if you're able to set that up). OpenRowSets look like this:
SELECT t1.Col1
, t2.ColA
FROM server1Table t1
INNER JOIN OPENROWSET('SQLNCLI', 'Server=Server2;Trusted_Connection=yes;',
'SELECT t2.ColA, t2.TheId FROM dbname.dbo.tableName') AS t2
ON t1.TheId = t2.TheId
and then you can just join on 'a' as if it's a local table. Under the hood it's probably pulling all the data down to your local database, so you should consider adding WHERE to the inner query to restrict rows, and only get the columns you need.