Replace All data in the table with relationship - sql-server

I am stuck on how to replace all the data in a table that already has relationship.
I have to tables, Company and Contact. and the PrimaryKeys of these two tables are related to other tables.
Before, I do anything with Contact, I made a copy of Contact table (select * INTO ContactBK from Contact)
After I modified some data in Contact, now I like to replace all the data from ContactBK (orginal) back to Contact, but I could not.
I have tried to use Import in Server Management Studio and select "replace exists data", but it failed. I also can not delete all the data in Contact table and replace, because the ContactID is tied to others tables.

You could do a update from the ContactBAK table using a join approach. If the records are essentially the same with a few fields modified, this should work. For example:
UPDATE c SET c.FirstName = bak.FirstName FROM Contacts c
LEFT JOIN ContactsBAK bak ON c.ContactID = bak.ContactID
You'll have to modify the fields you want to update and match on the joins of course.

Related

SQL Joining journal tables to user tables

I have a MSSQL database with 3 tables: Journals, Customers, and UserAccounts.
I'm trying to query Journals for transactions per account manager. This table has a customer ID column that links to Customers.
The Customers table has a ACC_Manager column that links to UserAccounts via UserID.
Inside the UserAcounts table are first and last name columns.
So it would be
Select
Journal.amount,
Customer.name,
UserAccounts.first
From
Tables
where
Journal.ACC_manager = 'Matt'
I'm having issues joining the tables so I can query using UserAccounts.first. Could anybody help? Thanks
Try the following. I didn't get exact column names so don't just use this code without modifying it a bit to suit your specific needs:
SELECT
j.amount,
c.name,
u.first
FROM
Journals j
JOIN Customers c ON
c.customerID = j.customerID -- Exact column names?
JOIN UserAccounts u ON
u.UserID = c.ACC_Manager
WHERE
u.first = 'Matt'
You may also need to use LEFT JOIN as opposed to JOIN. Read up on JOINs to be sure.

How to create a report based on multiple tables?

Here is the setup:
I have a database with about 7 tables. one of table (tblUsers) stores the names of the users of the database while the other six tables are used to store information inputted by the users of the database. with that I have a one-to-many relationship between the tblUsers and all the other tables in the database showing "ALL records from 'tblUsers' and only those records from 'OtherTable' where the joined fields are equal."
I have created a form (frmReport)with a combobox select a user from tblUsers and two text box that will allow me to select a date range.
Here is what I would like to accomplish: I would like to create a report that will return information from all the six tables based on criteria in the form.
PS. Im able to create the a report based on two tables eg. tblUsers and one other table, but whenever I have more that 2 table that's when I'm confused. I hope I'm clear enough.
Use one query with multiple LEFT JOINs such as:
SELECT tblUsers.*, tblA.*, tblB.*, tblC.*, tblD.*, tblE.*, tblF.*
FROM tblUsers
LEFT JOIN tblA ON tblUsers.pk=tblA.fk
LEFT JOIN tblB ON tblUsers.pk=tblB.fk
LEFT JOIN tblC ON tblUsers.pk=tblC.fk
LEFT JOIN tblD ON tblUsers.pk=tblD.fk
LEFT JOIN tblE ON tblUsers.pk=tblE.fk
LEFT JOIN tblF ON tblUsers.pk=tblF.fk
The LEFT JOIN includes only records from the first table and records that match on the second table. In this case I refer to pk as the primary key in tblUsers, and fk as the associated foreign key in the other tables. You can replace your table names in this query example, of course.
This will create one long record associated with each user in the tblUser table, but no records not matching the user.

how to select values from two tables

I have two tables arts and artsdetails.. artsId is the primary key in arts table foreign key in artdetails table. I am selecting values from both table but my query is giving error as
"Invalid object name 'artdetails'"
My query is:
SELECT arts.artsId, artdetails.mainImage
FROM artdetails
INNER JOIN arts ON artdetails.artsId = arts.artsId;
Please help.
You're probably not running the query in the database where these tables live.
If you're using SQL Server Management studio, look in the top left for a drop down containing the database names. It probably says 'master' (as that's the default). Select the one containing the tables you're using and re-run your query.
Failing that, check they're both running in the same schema as Tom suggests.
You can fully quality table names in your query like this:
SELECT a.artsId, ads.mainImage
FROM [DBNAME].[SCHEMA].artdetails ad
INNER JOIN [DBNAME].[SCHEMA].arts a ON ad.artsId = a.artsId;
Also using table abbreviations tidies things up a bit.

sql server: looking for table usage through out database

How would I figure out what type of sql code such as procs, functions, views etc. are interacting with my table called TABLE1 through out a given database. Sample code would be very helpful for me.
thanks
select so.name, so.xtype
from sysobjects so (nolock)
inner join syscomments sc (nolock) on sc.id = so.id
where sc.text like '%tablename%'
This code will search all SQL Server objects for a reference to your table. You have to run this query for each database.
If a stored procedure uses your table it will appear in this query. The same is true of functions, views, and triggers.
xtype tells you the type of object.
Here are the possible xtype values:
D = Field names
F = Foreign Key
FN = Function
P = Stored Procedures
PK = Primary Key
S = System Tables
U = User tables
V = Hidden tables
Not enough info in your question, but one thing you can do is use SQL Profiler to profile where INSERTs, UPDATEs, and DELETEs are coming from.
I assume you are talking about how an app is interacting with data and what name (of say a sproc) is doing the insert / update / delete.
Look at SQL Profiler, it comes with your client tools install. Filter it to only show connections to your database (either db name or ID).
If you've been good and created your SPs/views/functions after your table was created, sp_depends will tell you evertyhing referencing the table. Exept for dynamic sql that is.

TSQL: Create a view that accesses multiple databases

I have a special case,
for example in table ta in database A, it stores all the products I buy
table ta(
id,
name,
price
)
in table tb in database B, it contain all the product that people can buy
table tb(
id,
name,
price
....
)
Can I create a view in database A to list all the products that I haven`t bought?
Yes you can - the t-sql syntax is the same as within any other cross database call (within a stored procedure for example).
To reference your tables in the second database you simply need:
[DatabaseName].[Schema].[TableName]
So you would end up with something like
CREATE VIEW [dbo].[YourView]
as
select
a.ID,
a.SomeInfo,
b.SomeOtherInfo
from TableInA a
join DatabaseB.dbo.TableInB b
on -- your join logic goes here
Note that this will only work on the same server - if your databases are on different servers them you will need to create a linked server.
As the other answers indicate, you can use the {LINKED_SERVER.}DATABASE.SCHEMA.OBJECT notation.
You should also be aware that cross-database ownership chaining is disabled by default.
So within a database, granting SELECT on a view allows a user who may not have SELECT on the underlying tables to still SELECT from the view. This may not work across to another database where the user does not have permissions on the underlying table.
Yes, views can reference three part named objects:
create view A.dbo.viewname as
select ... from A.dbo.ta as ta
join B.dbo.tb as tb on ta.id = tb.id
where ...
There will be problems down the road with cross db queries because of backup/restore consistency, referential integrity problems and possibly mirorring failover, but those problems are inherent in having the data split across dbs.

Resources