I need to insert 26K rows of values into a temp table but run into 1,000 limit error. Is there any work around to this?
I will need to this type of insert many times and need to use temp tables for all my work.
IF OBJECT_ID('tempdb..#Budget') IS NOT NULL
BEGIN
DROP TABLE #Variables
END
CREATE TABLE #Budget
(
[SBU] VARCHAR(3),
[B_Type] VARCHAR(50),
[D_Type] VARCHAR(50),
[C_Direct] VARCHAR(50),
[Dest] VARCHAR(50),
[D_Month] VARCHAR(50),
[D_Year] INT,
[Pax] INT,
[Rev] INT
)
INSERT INTO #Budget ([SBU], [B_Type], [D_Type], [C_Direct], [Dest], [D_Month], [D_Year], [Pax], [Rev])
VALUES (.....)
Related
I am working on a project where I need to create a trigger that prints a statement each time an insert is performed on a table called enrolled.
This trigger is expected to print the faculty name, meeting location, time, student name after an insert is performed but when I run this trigger, it completes but after an insert is performed on the enrolled table, it does not print any information but the insert does complete and populates the table with the values. this is the test code I created below, along with the tables required to pull each value and a sample insert statement. any help is greatly appreciated.
USE test
GO
CREATE TRIGGER afterinsertprint on test.dbo.enrolled
AFTER INSERT
AS
BEGIN
DECLARE #studentname VARCHAR(30), #meetat varchar(30), #facultyname VARCHAR (30), #location NVARCHAR(10), #studentnumber int, #coursename NVARCHAR(12)
SELECT #studentnumber = snum, #coursename = cname FROM INSERTED;
SET #studentname = (SELECT sname from test.dbo.student WHERE snum = #studentnumber)
SELECT #facultyname = FNAME from test.dbo.faculty f join test.dbo.student s ON s.DEPT_ID = f.DEPT_ID where s.snum in
(SELECT snum FROM test.dbo.enrolled WHERE snum = #studentnumber and CNAME = #coursename);
SELECT #meetat = meets_at, #location = room FROM test.dbo.class WHERE cname IN (SELECT cname FROM test.dbo.enrolled
WHERE snum = #studentnumber and cname = #coursename);
IF (#studentnumber IS NOT NULL and #coursename IS NOT NULL)
BEGIN
PRINT (#studentname + #facultyname + #meetat + #location)
END;
END;
Below are the DML/DLL for table creation/insert
CREATE TABLE dbo.STUDENT
(snum int,
sname nvarchar(12),
MAJOR nvarchar(12),
DEPT_ID int,
slevel nvarchar(12),
AGE int,
CONSTRAINT STUDENT_SNUM_pk PRIMARY KEY (SNUM));
--
CREATE TABLE dbo.CLASS
(CNAME nvarchar(12),
MEETS_AT time,
ROOM nvarchar(10),
FID int,
CONSTRAINT CLASS_CNAME_pk PRIMARY KEY (CNAME));
--
CREATE TABLE dbo.ENROLLED
(SNUM int,
CNAME nvarchar(12));
--
CREATE TABLE dbo.FACULTY
(FID int,
FNAME nvarchar(40),
DEPT_ID int,
CONSTRAINT FACULTY_FID_pk PRIMARY KEY (FID));
--
CREATE TABLE dbo.DEPARTMENT
(DEPTid int,
DNAME nvarchar(100),
LOCATION nvarchar(100),
CONSTRAINT DEPARTMENT_DEPTID_pk PRIMARY KEY (DEPTID));
insert into dbo.STUDENT values (1234,'T.Banks','ME',3,'SR',19);
insert into dbo.DEPARTMENT values (1,'Computer Sciences','West Lafayette');
insert into dbo.FACULTY values (107,'Y.Walton',1);
insert into dbo.CLASS values ('ENG400',cast('08:30' as time),'U003',107);
insert into dbo.ENROLLED values (1234,'ENG400');
I have these tables in my SQL Server database:
CREATE TABLE weather
(
weatherId integer Identity(1,1) primary key,
weatherDate datetime,
rainout BIT,
temperature float,
inchesOfRain float
)
CREATE TABLE weather_audit
(
weatherAuditId integer Identity(1,1) primary key,
weatherId integer,
date datetime,
rainout BIT,
temperature float,
inchesOfRain float
)
CREATE TABLE maintenance
(
maintenanceId integer Identity(1,1) primary key,
maintenanceDescription nvarchar(100),
dateRequested datetime,
dateResolved datetime,
currentStatus nvarchar(20),
estimatedCost decimal,
)
CREATE TABLE maintenence_audit
(
mainteneceAuditId integer Identity(1,1) primary key,
maintenanceId integer,
description nvarchar(100),
dateRequested datetime,
dateResolved datetime,
currentStatus nvarchar(20),
estimatedCost decimal,
updatedOn datetime
)
I want to set up a trigger which fires when a row is inserted into the weather table whose inchesOfRain is > 4. This is what I have now, but it isn't working:
CREATE TRIGGER tr_weather_ForInsertUpdate
ON weather
FOR INSERT, UPDATE
AS
BEGIN
SET NOCOUNT ON
IF (SELECT TOP 1 inchesOfRain FROM weather) > 4
INSERT INTO weather_audit (weatherId, weatherDate, rainout, temperature, inchesOfRain)
SELECT
i.weatherId, i.weatherDate, i.rainout, i.temperature, i.inchesOfRain
FROM
Inserted AS I
END
So if I were to insert this
INSERT INTO dbo.weather (weatherDate, rainout, temperature, inchesOfRain)
VALUES ('4/21/2018', '0', '70', '6');
it would add a row to the weather table and also to the weather_audit table since the inches of rain is 6
You need to depend on your Inserted pseudo table to make the decision whether or not to add a row to the audit table - try this:
CREATE TRIGGER tr_weather_ForInsertUpdate
ON weather
FOR INSERT, UPDATE
AS
BEGIN
SET NOCOUNT ON
INSERT INTO weather_audit (weatherId, weatherDate, rainout, temperature, inchesOfRain)
SELECT
i.weatherId, i.weatherDate, i.rainout, i.temperature, i.inchesOfRain
FROM
Inserted AS I
WHERE
i.inchesOfRain > 4 -- just insert those rows with inchesOfRain > 4
END
Your trigger is all good, it just needs to be
CREATE TRIGGER tr_weather_ForInsertUpdate
ON weather
FOR INSERT, UPDATE
AS
BEGIN
SET NOCOUNT ON
--Modified Code Start
DECLARE #inchesOfRain float
SELECT #inchesOfRain = inchesOfRain FROM inserted
IF #inchesOfRain > 4
BEGIN
---------------Modified COde End
INSERT INTO weather_audit (weatherId, weatherDate, rainout, temperature, inchesOfRain)
SELECT
i.weatherId, i.weatherDate, i.rainout, i.temperature, i.inchesOfRain
FROM
Inserted AS I
END
END
Update: Marc S code snippet is the perfect answer.
I have a table Hub:
and second table Rates:
In this, FromHubId and ToHubId are foreign keys from Hub table
I wanna add some data from a file in Rates table. What I have tried so far is:
Create a temp Table and insert values in it:
CREATE TABLE #Table
(FromHub varchar(30),
ToHub varchar(30),
Rate float,
rate40 float,
)
INSERT INTO #Table values('AUCKLAND','AUCKLAND',229.157325588818,341.973239724851),
('AUCKLAND','BLENHEIM',1825.03244430479,2738.13624934331),
('AUCKLAND','CHRISTCHURCH',1977.80399469734,2966.11840915988),
('AUCKLAND','DUNEDIN',2280.99676393793,3422.08272879301),
('AUCKLAND','GREYMOUTH',2432.59314855822,3650.06488860958),
('BLENHEIM','AUCKLAND',1520.66450929195,2280.99676393793),
('BLENHEIM','BLENHEIM',229.157325588818,341.973239724851),
('CHRISTCHURCH','AUCKLAND',1748.64666910852,2622.97000366278),
('CHRISTCHURCH','DUNEDIN',911.92863926627,1367.89295889941),
('CHRISTCHURCH','GREYMOUTH',685.121645221953,1028.27005071905),
:
:
:;
Create another Temp Table and trying to insert values in it:
CREATE TABLE #Table1
(FromHubId uniqueidentifier,
ToHubId uniqueidentifier,
Rate float,
Rate40 float,
FromHub varchar(30),
ToHub varchar(30)
);
insert into #Table1
select h.HubId As FromHubId, h.HubId As ToHubId, t.Rate, t.Rate40, t.FromHub, t.ToHub
FROM #Table t JOIN Hub h ON
t.FromHub=h.Centre and t.ToHub=h.Centre;
select * from #Table1;
But, It only insert values where FromHub and ToHub are same. I want to insert all values i.e. for different From and To hub as well.
Any help will be appreciated. Thanks..!!
TRY THIS: you have to join separately fro both from and to hubId using table alias as below:
INSERT INTO #Table1
SELECT DISTINCT h.HubId As FromHubId,
h1.HubId As ToHubId,
t.Rate,
t.Rate40,
t.FromHub,
t.ToHub
FROM #Table t
JOIN #Hub h ON t.FromHub=h.Centre
JOIN #Hub h1 ON t.ToHub=h1.Centre;
I have the following code and it give me an error when the table #ListaDeProducto has more than 1 row. Any idea?
insert into Solicitud_Plastico_Interna_Detalle(
IDSolicitud_Plastico_Interna
,IDTipo_Producto
,Cantidad_Solicitada
,Create_User
,Create_Date
,Contingencia
,Total
)
select
#IdSolicitud
,IDTipo_Producto
,Cantidad_Requerida
,#USUARIO
,getdate()
,Contingencia
,Total
from #ListaDeProducto
Table schema
CREATE TYPE [ListaProductoTableType2] AS TABLE
(
IDTipo_Producto int,
Tipo_Producto varchar(1000),
Cantidad_Requerida int,
Contingencia int ,
Total int,
IdSolicitud_batch varchar(100)
)
GO
I still will bet there is some trigger in the table.
So why you dont try create a new table to prove this query is ok with multiple rows
CREATE TABLE Solicitud_Plastico_Temporal AS (
select
#IdSolicitud as IDSolicitud_Plastico_Interna
,IDTipo_Producto
,Cantidad_Requerida
,#USUARIO as Create_User
,getdate() as Create_Date
,Contingencia
,Total
from #ListaDeProducto
)
Basically I've created a table in SQL Server:
CREATE TABLE [Books]
(
[store] int,
[sgxg] int,
[t] varchar(50),
[movie] varchar(50),
[year] int
);
but when I selected the rows from my table books, there was nothing there even though I refreshed it. 5 columns were created, but no data was inserted??
Your table is defined as:
CREATE TABLE [Books]
(
[store] int,
[sgxg] int,
[t] varchar(50),
[movie] varchar(50),
[year] int
);
Your procedure is almost correct. Try this:
CREATE PROC [InsertBooks]
AS
BEGIN
BULK INSERT [Books]
FROM 'C:\Users\dachen\workspace\612Lab3(XMLFinalVersion)\books.txt'
WITH (
FIRSTROW=2,
FIELDTERMINATOR=',',
ROWTERMINATOR='\n'
)
END;
Execute the procedure as follows:
EXECUTE [InsertBooks];
View the data inserted:
SELECT * FROM [Books]