Trying to figure out how a table in SQL Server is getting populated - sql

There is no documentation and no way to get in touch with the architects of the table. Are there any queries or options in SSMS that allows me to know what the table is connected to ?
A brief explanation of my issue is that there are two tables that seem to be connected.
Table A which contain 10M distinct users and Table B 5M users. All users in Table B are in Table A but obviously Table A's users aren't in Table B. After running multiple tests on the website, I cant find the trigger that copies the user information from Table A to Table B. And that's why I would like to know how Table B gets populated.

Like Diado already said, try running a profiler for a while. It will log queries against the database (e.g. INSERT ... and UPDATE ...), the source of the connection (e.g. IP address), and sometimes the process name. That could provide clues into what's interacting with your table in question.
https://learn.microsoft.com/en-us/sql/tools/sql-server-profiler/sql-server-profiler?view=sql-server-2017

Related

Access SQL Server Insert blocked by Select

OK we have a multi user (25 users) Access 2013 FE and a SQL Server 2012 BE. Up until yesterday the whole system was working FINE and now it has completely stopped.
If user A has a record open via a straight forward select query reading from TABLE Z, then if user B tries to do an insert on TABLE Z, they receive a timeout message. When I go to SQL server and run SP_WH02, it states User B is blocked by User A. When I then investigate the command that is blocking user B, it is just a simple SELECT statement.
Does anyone know why this would be?
The form that User A has open has Record Locks = No Locks and Recordset Type = Dynaset
The record source is a SELECT, retrieving two fields where the key field is a parameter based on the value of another.
However, nothing has changed on this system for months, so I'm confused as to why this would happen.
Thanks for any help.
Ok we solved it. If anyone else has the same issue: We archived 9400 records into a new table and now we can do inserts again. It has bought us time so going forward I will normalize table Z further and auto archive records against a criteria.
It may be the same or similar issue as in MS Access holds locks on table rows indefinitely
Access only fetches the first x rows of the big recordsource, leaving the table in a ASYNC_NETWORK_IO wait state, i.e. locked.
Possible solutions are:
Don't have forms or queries that select all records. It usually doesn't make too much sense to scroll through 20k+ records.
Force Access to fetch all records, to release the lock. You can do this with Me.RecordsetClone.MoveLast e.g. in Form_Load(). Only advisable with a fast network connection.

SSIS - insert data/ID into another table while importing

I'm working with SQL Server / SSIS 2005 and I am stumped on something that I believe should be a simple issue. I am trying to figure out how to insert data into another table from an incremental ID created within an SSIS project.
To help clarify, there is my example:
I have a table called users with the following values (shortened for this purpose)
User ID Username
======= ========
1 jsmith
2 jjones
I have another table called userpreferences with the following values
ID Keyname Keyvalue
======= =========== =========
1 Send a Report YES
2 Send a Report YES
Now that I have described the tables, I am going to be using SSIS to insert data into the users table. User ID is an identity field in the users table. ID is also an identity field in the userspreferences table. And they each correspond with each other.
What I would like to do is insert data into the userpreferences table based upon what the User ID is being generated in the users table. As an example, I insert a record through import as user ID #3. I then want to insert that ID to the user preferences table and insert the keyname and keyvalues. Just to clarify, the keyname and keyvalues are not part of the text file. I want to insert those in within the project.
Currently, I can achieve my goal through some "post processing" through T-SQL. But I am trying to do this more efficiently in SSIS. Plus this would help me a lot as I do this quite frequently.
I tried researching this and I thought this may help me: SSIS - Multiple table insert. However, the solution has the screen shots missing. Can someone assist me with this task. I would greatly appreciate it.
You could turn identity insert on your users table, and manually generate the IDs using a VB script component transformation, so they'd be available to you in the data flow to load to the user preferences table. Post processing is probably your best option here though - just build an Execute SQL task to run after the data flow completes and it will all still be part of your package.

Can I do a INSERT using select from another DB with the same table name

I have a person table (sqlServer 2008r2) and are using .vbs and .bat files to sync some data from a table called person in database A to database B.
Note - The DB names are different but the table names are the same. Because the peson table has 137 fields I am looking for a way to write both an INSERT and an UPDATE statement. How can I do thsi without listing 137 fields?
At the moment I connect to database A and populate a recordSet called RS with the people table records.
Then I loop through RS and query the people table in database B
If found I update people table in database B with the people table record from database A
If not found I insert the people record from database A into the people table on database B
Now this is nice and easy but since it has 137 fields I do not want to write a MASSIVE update and insert statements. DO I have another option such as:
The table structures are identical between people on database A and people on database B but the recordset obtained in step 1 above is using one one DB connection and the query in step 2 is using a separate DB connection to a different DB instance on a different server.
Help.
You can do that by using a fully qualified name.
insert into DATABASE1.dbo.Person (columns)
select (columns) from DATABASE2.dbo.Person
Also there are others way to pump data from a database for another
You can take a look on your MSSMS it got a tool for that kind of thing.
Select one database and right button mouse to open the import/export wizard.
You can also take a look on replication, bulk copy, service broker, etc...

How to delete a large record from SQL Server?

In a database for a forum I mistakenly set the body to nvarchar(MAX). Well, someone posted the Encyclopedia Britanica, of course. So now there is a forum topic that won't load because of this one post. I have identified the post and ran a delete query on it but for some reason the query just sits and spins. I have let it go for a couple hours and it just sits there. Eventually it will time out.
I have tried editing the body of the post as well but that also sits and hangs. When I sit and let my query run the entire database hangs so I shut down the site in the mean time to prevent further requests while it does it's thinking. If I cancel my query then the site resumes as normal and all queries for records that don't involve the one in question work fantastically.
Has anyone else had this issue? Is there an easy way to smash this evil record to bits?
Update: Sorry, the version of SQL Server is 2008.
Here is the query I am running to delete the record:
DELETE FROM [u413].[replies] WHERE replyID=13461
I have also tried deleting the topic itself which has a relationship to replies and deletes on topics cascade to the related replies. This hangs as well.
Option 1. Depends on how big the table itself and how big are the rows.
Copy data to a new table:
SELECT *
INTO tempTable
FROM replies WITH (NOLOCK)
WHERE replyID != 13461
Although it will take time, table should not be locked during the copy process
Drop old table
DROP TABLE replies
Before you drop:
- script current indexes and triggers so you are able to recreate them later
- script and drop all the foreign keys to the table
Rename the new table
sp_rename 'tempTable', 'replies'
Recreate all the foreign keys, indexes and triggers.
Option 2. Partitioning.
Add a new bit column, called let's say 'Partition', set to 0 for all rows except the bad one. Set it to 1 for bad one.
Create partitioning function so there would be two partitions 0 and 1.
Create a temp table with the same structure as the original table.
Switch partition 1 from original table to the new temp table.
Drop temp table.
Remove partitioning from the source table and remove new column.
Partitioning topic is not simple. There are some examples in the internet, e.g. Partition switching in SQL Server 2005
Start by checking if your transaction is being blocked by another process. To do this, you can run this command..
SELECT * FROM sys.dm_os_waiting_tasks WHERE session_id = {spid}
Replace {spid} with the correct spid number of the connection running your DELETE command. To get that value, run SELECT ##spid before the DELETE command.
If the column sys.dm_os_waiting_tasks.blocking_session_id has a value, you can use activity monitor to see what that process is doing.
To open activity monitor, right-click on the server name in SSMS' Object Explorer and choose Activity Monitor. The Processes and Resource Waits sections are the ones you want.
Since you're having issues deleting the record and recreating the table, have you tried updating the record?
Something like (changing "body" field name to whatever it is in the table):
update [u413].[replies] set body='' WHERE replyID=13461
Once you clear out the text from that single reply record you should be able to alter the data type of the column to set an upper bound. Something like:
alter table [u413].[replies] alter column body nvarchar(100)

Export/View data from a SQL Server temporary table

I have a temporary table, that isn't going away. I want to see what is in the table to determine what bad data might be in there. How can I view the data in the temporary table?
I can see it in tempdb. I ran
SELECT * FROM tempdb.dbo.sysobjects WHERE Name LIKE '#Return_Records%'
to get the name of the table.
I can see it's columns and its object id in
select c.*
from tempdb.sys.columns c
inner join tempdb.sys.tables t ON c.object_id = t.object_id
where t.name like '#Return_Records%'
How can I get at the data?
By the way, this doesn't work
SELECT * FROM #Return_Records
One way of getting at the data in a low-level and not particularly easy to manipulate manner is to use the DBCC PAGE command as described in a blog post by Paul Randal:
http://blogs.msdn.com/sqlserverstorageengine/archive/2006/06/10/625659.aspx
You should be able to find the fileid and page number of the first page in the object by querying on sysindexes .. the last time I did this was on SQL Server 7.
If the data is in the database, then DBCC page will be able to dump it.
pjjH
SQL Server limits access to Local Temp Tables (#TableName) to the connection that created the table. Global temp tables (##TableName) can be accessible by other connections as long as the connection that created it is still connected.
Even though you can see the table in the table catalog, it is not accessible when trying to do a SELECT. It gives you an "Invalid Object Name" error.
There's no documented way of accessing the data in Local Temp Tables created by other connections. I think you may be out of luck in this case.
This is something that seems like you obviously tried, but since you didn't mention it I though I would mention just in case:
Did you try "SELECT * FROM #Return_Records"?
Like José Basilio says, that's a temporary table belonging to another connection. If it's there for a long time, it must belong to a connection that has been open for a long time. Check Maintenance -> Acitivity Monitor; you can sort by Login Time.
Check if the Login Time, or Last Batch Time, matches with the create date of the temporary table. That can be retrieved with:
select crdate from tempdb.dbo.sysobjects WHERE Name LIKE '#Return_Records%'
You can shoot down suspect connections (right click and Kill Process.) If the table is gone after killing a process, you've found the culprit.
To just remove the table, restart the SQL Server service. You can attach SQL Profiler right after with a filter to start looking for the connection that creates the temporary table.