Where did my new table go - sql-server

I have a database called mbt. I wanted to write some data from temporary table to real table.
--I used this query.
SELECT * INTO new_table FROM #tmp
when i runned the query it returned normal message.
15813 row(s) affected
After that i checked my tables in mbt database, but i couldn't see 'new_table'
how could such a thing be, where the table might have gone.
I may have forgotten to use 'use MBT' statment at the beginning of the query. Does it make problem
I'm using ms sql server 2014(SP2)(KB3171021)-12.0.5000.0(X64)
ANSWER
It gone to Master DB
select 'master' as DatabaseName,
T.name collate database_default as TableName
from master.sys.tables as T

It Will create a new table on your database. but you did not use so it will store in master database on your server.

Run the query below to find databases which have the object new_table:
sp_MSForEachDB 'Use [?] IF EXISTS (SELECT 1 FROM sys.objects WHERE name= ''new_table'')
SELECT DB_NAME()'

I had the same problem. What i did is, I rewrite the statement of use Database and then refresh the database browser after that i got Result. You can try it. may be it will help you.

Always use command "USE db_name" to make sure that you are querying right database.
Below command will show all databases available on the server.
SHOW DATABASES;
If you are using GUI tool to connect DB server, there is a possibility that at the time of connection you got connected to different DB. If you executed the query to create table and inserted record. These records are inserted in new table in different DB than mbt.

Related

How to loop on multiple databases and tables

I have a SQL Server instance which contains multiple databases. I have 2 tables which exist in all databases on the server:
Refresh Log
Detailed refresh log.
I want to union all the tables across all databases on the server so the final result will be 2 tables which are the union refresh log and detailed refresh log.
I need help to write the function which runs across all databases.
I'm also a little uncertain as to what you're hoping for, for example if you need the resulting output to be in two permanent tables or if you just need the result when queried. Of course once you build your SELECT you can return it to the caller or put it into a table, so I'll leave that up to you.
If your databases are unchanging, then of course you can just write your query and maybe put it into a VIEW for convenience:
SELECT columns from database1.dbo.RefreshLog
UNION ALL
SELECT columns from database2.dbo.RefreshLog
...
and so on
But if you're saying that your databases are themselves dynamic, in other words that databases may be created or dropped over the lifetime of your project, then you could consider using the "undocumented" procedure sp_msforeachdb to build up a list of databases, and then use THAT list to build your UNION query. Here's a quick script that captures the names of all databases that include a specific table ("Products" in the example):
IF object_id('tempdb..#DatabaseNames') IS NOT NULL
DROP TABLE #DatabaseNames
CREATE TABLE #DatabaseNames (DatabaseName SYSNAME)
execute sp_msforeachdb #command1=
N'IF EXISTS(SELECT * FROM [?].sys.tables WHERE Name = ''Products'')
INSERT #DatabaseNames VALUES(N''Database [?]'')'
SELECT * FROM #DatabaseNames

How to find/see tempdb in SQL Server

Should I expect to be able to see tempdb tables in SSMS?
e.g. If I run this code, should I expect to be able to see the table in SSMS?
-- Drop the table if it already exists
IF (SELECT OBJECT_ID('tempdb..#TempPasswords')) IS NOT NULL
DROP TABLE #TempPasswords
CREATE TABLE #TempPasswords (
MemberNo INT,
Password nvarchar(120),
NewPassword varbinary(MAX)
)
Only if you run your code snippet in SSMS. In that session you will be able to execute
T-SQL that include your temp table.
In order to see temp tables globally use two hashes ##.
It is possible to see the temp DB but the name only. To see temp DB follow the instruction as per the image. After running the query right click on the Temporary Tables and Refresh .

What can I do with the results of EXEC (sql) AT LinkedServer?

I am using the EXEC (sql) AT LinkedServer command to get some data from a remote server. So far the only thing I can do with the results in my script is insert them into a local table.
CREATE TABLE #MyTable (Col1 VARCHAR(10))
INSERT INTO #MyTable
EXEC ('SELECT Col1 FROM MyDB.dbo.Table1 WHERE Col2 = ?', 'Val2') AT REMOTEDDB
This works great and I can use this.
I wanted to join the result set to a local table directly like OPENQUERY does but I can't find syntax for that. It may not be supported. I can't use OPENQUERY because the data is too big and I need to apply run time parameters.
Are there any other options for processing and working with the results of the EXEC AT command that don't require inserting it into a table first?
There is no way, that I know of, to join directly to an Exec. Inserting into a temp table would be your best bet in my opinion. Even if you joined directly in syntax the same thing would still happen, SQL Server needs to pull the remote data local and then perform the join.
Another option might be to pass a table parameter and have that output. You can do that with sp_executesql.
https://msdn.microsoft.com/en-us/library/ms188001.aspx
I would sync that table locally using something like sql agent every 5 minutes or so. Then I do the join to that local table.
In my experience, querying remote table using link server never gives me the best query plan available. I would avoid that if I can.
There is nothing wrong in caching remote results locally before using them. On average, this approach gives the best performance.
Also, you can try a static query:
select *
from dbo.LocalTable lt
inner join RemoteDB.MyDB.dbo.Table1 rt on lt.Id = rt.Id
where rt.Col2 = 'Val2';
If it wouldn't work straight away, at least you can try REMOTE join hint. Of course, normally it should be a last resort (the hint, I mean).

Select From SQL Server Stored Procedure Resutls

I am migrating several hundred stored procedures from one server to another, so I wanted to write a stored procedure to execute an SP on each server and compare the output for differences.
In order to do this, I would normally use this syntax to get the results into tables:
select * into #tmp1 from OpenQuery(LocalServer,'exec usp_MyStoredProcedure')
select * into #tmp2 from OpenQuery(RemoteServer,'exec usp_MyStoredProcedure')
I then would union them and do a count, to get how many rows differ in the results:
select * into #tmp3
from ((select * from #tmp1) union (select * from #tmp2))
select count(*) from #tmp1
select count(*) from #tmp3
However, in this case, my stored procedure contains an OpenQuery, so when I try to put the exec into an OpenQuery, the query fails with the error:
The operation could not be performed because OLE DB provider "SQLNCLI"
for linked server "RemoteServer" was unable to begin a distributed transaction.
Are there any good workarounds to this? Or does anybody have any clever ideas for things I could do to make this process go more quickly? Because right now, it seems that I would have to run the SP on each server, script the results into tmp tables, then do the compare. That seems like a poor solution!
Thank you for taking the time to read this, and any help would be appreciated greatly!
I think your method would work - you just need to start the MSDTC. This behavior occurs if the Distributed Transaction Coordinator (DTS) service is disabled or if network DTC access is disabled. By default, network DTC access is disabled in Windows. When running and configured properly, the OLE DB provider would be able start the distributed transaction.
Check out this for instructions- it applies to any Windows Server 2003 or 2008.
Similar to your question.
Insert results of a stored procedure into a temporary table

getting schema + first 100 records from every table in a db

i have a large sql server db, and i want to get the schema (all tables/triggers/sprocs), i'm pretty sure that's easy.
but the tough part is that i want to get 100 records from each table. it's a huge db on a remote server and i can't develop locally without a mockup copy.
thanks for your help!
To get the schema, basically just select everything from the sys.objects catalog view:
SELECT * FROM sys.objects
For the data: you could use the undocumented (but extremely helpful) stored procedure sp_MSForEachTable for that purpose:
exec sp_MSforeachtable 'select top 100 * from ? '
I would create a cursor with sys.objects to get the user defined tables and populate the new database with select query of top 100 rows.
Make sure you have (NOLOCK) hint to your query, so that it can avoid locks

Resources