MSSQL server select into - sql-server

I have a table with 20 millions rows and I need to create another table
off of it. but select into new table only inserts 100K records.
I have tried select into it and select table as
select * INTO #test
from dbo.log
New table should have 20 millions rows

Tables in temp db are very similar to the one available on regular databases. The table may be restricted with disk space available.
SELECT *
INTO #temp
FROM dbo.log
-- checking actual number of rows affected by query above
SELECT ##ROWCOUNT;

In addition to #gordy's ROWCOUNT suggestion, you could be looking at the wrong #temp table.
Consider:
select top 10 * into #foo from sys.objects
exec ('select * into #foo from sys.objects')
select count(*) from #foo
The nested batch creates a transient temp table called #foo that is destroyed at the end of the batch. This would happen calling a stored procedure too.

Related

SQL Server - View or Stored Proc to Union All On a Growing Table List

The SQL server has tables that all have the same structure. tableX_2016, tableX_2017, tableX_2018, tableX_2019, tableX_2020, tableX_2021.
I want a view that is simply:
select * from tableX_2016
union all select * from tableX_2017
union all select * from tableX_2018
union all select * from tableX_2019
union all select * from tableX_2020
union all select * from tableX_2021
However, I would like that view to automatically pick up tableX_2022, tableX_2023, etc., once they exist. The best would be to put all this data in one table (!), but unfortunately, I do not have the latitude to do that.
Is there a way to do this in a view? Or, a way to do it in a stored procedure and then somehow consume that stored procedure as if it were a view (use its output in CTEs, join to its output, etc)?
Thanks!
Database design seems poor. If you are keeping data in same database, it would be wise to handle by creating year or date column.
However, you can achieve your result by the given query
DROP TABLE IF EXISTS ##temp
SELECT * INTO ##temp FROM dbo.tableX_2016
TRUNCATE TABLE ##temp
EXEC sp_MSforeachtable
#command1 = 'INSERT INTO ##temp SELECT * FROM ?',
#whereand = 'AND OBJECT_NAME(OBJECT_ID) LIKE ''tableX_%'''
SELECT * FROM ##temp
Note that, sp_MSforeachtable is an undocumented stored procedure in SQL Server
PS: You can create view, stored procedure, etc. as per the need using above query

What is easiest and optimize way to find specific value from database tables?

As per my requirement, I have to find if some words like xyz#test.com value exists in which tables of columns. The database size is very huge and more than 2500 tables.
Can anyone please provide an optimal way to find this type of value from the database. I've created a loop query which took around almost more than 9 hrs to run.
9 hours is clearly a long time. Furthermore, 2,500 tables seems close to insanity for me.
Here is one approach that will run 1 query per table, not one per column. Now I have no idea how this will perform against 2,500 tables. I suspect it may be horrible. That said I would strongly suggest a test filter first like Table_Name like 'OD%'
Example
Declare #Search varchar(max) = 'cappelletti' -- Exact match '"cappelletti"'
Create Table #Temp (TableName varchar(500),RecordData xml)
Declare #SQL varchar(max) = ''
Select #SQL = #SQL+ ';Insert Into #Temp Select TableName='''+concat(quotename(Table_Schema),'.',quotename(table_name))+''',RecordData = (Select A.* for XML RAW) From '+concat(quotename(Table_Schema),'.',quotename(table_name))+' A Where (Select A.* for XML RAW) like ''%'+#Search+'%'''+char(10)
From INFORMATION_SCHEMA.Tables
Where Table_Type ='BASE TABLE'
and Table_Name like 'OD%' -- **** Would REALLY Recommend a REASONABLE Filter *** --
Exec(#SQL)
Select A.TableName
,B.*
,A.RecordData
From #Temp A
Cross Apply (
Select ColumnName = a.value('local-name(.)','varchar(100)')
,Value = a.value('.','varchar(max)')
From A.RecordData.nodes('/row') as C1(n)
Cross Apply C1.n.nodes('./#*') as C2(a)
Where a.value('.','varchar(max)') Like '%'+#Search+'%'
) B
Drop Table #Temp
Returns
If it Helps, the individual queries would look like this
Select TableName='[dbo].[OD]'
,RecordData= (Select A.* for XML RAW)
From [dbo].[OD] A
Where (Select A.* for XML RAW) like '%cappelletti%'
On a side-note, you can search numeric data and even dates.
Make a procedure with VARCHAR datatype of column with table name and store into the temp table from system tables.
Now make one dynamic Query with executing a LOOP on each record with = condition with input parameter of email address.
If condition is matched in any statement using IF EXISTS statement, then store that table name and column name in another temp table. and retrieve the list of those records from temp table at end of the execution.

SQL Server, Insert into table from select query?

I would fill a table from a query in a stored procedure,
This works:
SELECT *
INTO #tmpTable
FROM MyTable
This works:
SELECT TOP (1) *
FROM MyTable
WHERE Land = #Land
but how do I fill #tmpTable with
SELECT TOP (1) *
FROM MyTable
WHERE Land = #Land
Because the #temp Table's scope is limited to its session (SPID), i.e. the Stored Procedure itself. After the SP execution completes the #temp table is Dropped.
Also while the SP is being executed you cannot see the #temp Table from other sessions (SPID)
USE Global temp table like ##temp even it can be accessible after execution of sp also

Temporary table in SQL Server - error occurs if the query takes long time to complete

Have a look a the following query.
select *
into TempTable
from MainTable
select * from TempTable
WAITFOR DELAY '00:00:10'
drop table TempTable
After executing this query, I open other window and execute the query:
select *
into TempTable
from MainTable
select * from TempTable
drop table TempTable
I get the following error:
There is already an object named 'TempBucket' in the database.
Suppose that it a part of the stored procedure, and it takes a long time to finish. If there's a second call to this stored procedure, this error will occur. How do I fix this error?
I assume you are using MSSQL DBMS by the tags against your post.
Use a genuine temp table: prefix the name of the table with '#'.
Using this method the temp table will exist only in the scope of the procedure within which it was created.
select *
into #TempTable
from MainTable
select * from #TempTable
No drop actually neccessary but is probably better practice.
Try this one -
IF OBJECT_ID (N'tempdb.dbo.#TempTable', 'U') IS NOT NULL
DROP TABLE #TempTable
SELECT *
INTO #TempTable
FROM dbo.MainTable
SELECT *
FROM #TempTable

Insert records into table in SQL server

Is there a way in SQL SERVER(05 & 08) that I can insert records from an external file into a temp table? I do not have privilege to the database at all. Here is what i tried to do:
CREATE table #temp
(KEY_ID INT)
INSERT INTO #temp
SELECT 90883000
Ran #temp table with result:
KEY_ID
---------
90883000
It kind of works with just one record. How do I do if I have hundred records? Thanks a lot!!!
This just to show how to add multiple rows in a table.
CREATE table #temp(
KEY_ID integer
)
Declare #i as int
set #i = 1
WHILE (#i <= 10000)
BEGIN
insert into #temp values(#i)
set #i += 1
End
How about a table variable. I believe that the #temp need rights to the tempdb database. I believe that a table variable is used just like any other variable which is session based.
To declare a table variable:
DECLARE #ProductTotals TABLE
(
ProductID int,
Revenue money
)
Insert into a table variable:
INSERT INTO #ProductTotals (ProductID, Revenue)
SELECT ProductID, SUM(UnitPrice * Quantity)
FROM [Order Details]
GROUP BY ProductID
For importing files, you can use BULK IMPORT. Use this to get the data into your temp table, then you can run set based operations against it.
A more effective way of inserting a lot of rows.
INSERT INTO #temp
(KeyID)
SELECT TOP 1000 -- PUT YOUR QUANTITY HERE
IDENTITY(INT,1,1) AS N
FROM Master.dbo.SysColumns sc1,
Master.dbo.SysColumns sc2
If you're going to be using this a lot, just ask them to create you a TALLY TABLE.
This is how I usually do it if I have the values in a file somewhere. Inserting hundreds of records from flat text file into temp table. My example only has one column, but as long as the flat file is delimited somehow, you can do as many columns as needed. Need to make sure you put the text file in a directory you have access to.
IF OBJECT_ID('tempdb..#TempTable') IS NOT NULL DROP TABLE #TempTable
CREATE TABLE #TempTable(Col1 varchar(10)) --Specify temp-table name & columns
BULK INSERT #TempTable
FROM 'C:\FileName.txt' --Specify file path/name
WITH (
FIELDTERMINATOR = '\t',
ROWTERMINATOR = '\n'
);
GO

Resources