Fix my SQL Server blank screen - sql

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]

Related

Insert values 1000 row limit

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 (.....)

Doing a for-each record in a user defined table type in Stored Procedure

:)
I have this defined type:
CREATE TYPE dbo.MyType
AS TABLE
(
name varchar(255),
value varchar(255)
);
Having this stored procedure:
CREATE PROCEDURE MyProcedure #docName varchar(255), #docPath varchar(255), #values AS [dbo].MyType Readonly
AS
declare #ID table (ID int)
INSERT INTO MyTable output inserted.fileID into #ID values (#docName,#docPath)
-- insert loop here
GO;
And the following "one to many" table
CREATE TABLE TableN (
fileID int PRIMARY KEY,
name varchar(255),
value varchar(255)
)
How can I, where it is noted in the above code, make a loop in order to for each record in the MyType table, to insert it into TableN, together with the fileID from the insert?
Thanks!
There's no need for a loop (you need to stop thinking programmatically in SQL and think in datasets). Just do an INSERT:
INSERT INTO TableN (FileID,[name],[value])
SELECT ID.ID,
V.[Name],
V.[value]
FROM #values v
CROSS JOIN #ID ID;

How to pass an int column to a procedure which take an int parameter

I have a stored procedure MyProcedure which needs an int as parameter, now I need to pass an int column from another table as parameter to the procedure. How can I write a statement to implement that? I can't use cursor in my code for some reasons. Below is my pseudo code. Thanks.
Create Procedure MyProcedure
#i_input INT
AS
... ...
create table MyTable
(
id int,
otherdata float
)
exec MyProcedure (select id from MyTable)
Go for function because you can't run Procedure through DML/DDL
CREATE TABLE MyResult
(
id INT,
results VARCHAR(MAX)
)
Go
CREATE FUNCTION MyFunction(#i_input INT)
RETURNS TABLE
AS RETURN
(
SELECT #i_input AS ID, 'result1' AS results
)
Go
INSERT INTO MyResult SELECT * FROM MyFunction(1)
You can do like this
Create Procedure MyProcedure
#i_input INT
AS
... ...
create table MyTable
(
id int,
otherdata float
)
Declare #temp_id int
select #temp_id =id from MyTable
exec MyProcedure #temp_id

Insert into select Subquery returned more than 1 value

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
)

Stored Procedure that updates fields with different values

I am using SQL Server.
I need to create a stored procedure that will update the Data field (table bellow) with different value for every ID value. (the values in the Data fields depend on the user input).
ID | Data
---------
1 | NULL
2 | NULL
3 | NULL
For example:
if ID = 1, Data should be "Test1"
The ID and Data pairs should somehow be input parameters to the stored procedures.
Is this possible, or I'll have to call simple update procedure for every ID/Data pair?
You need to use XML for sending data for multiple rows. For your current problem prepare (generate dynamically) an xml like below.
'<NewDataSet><Table><Id>1</Id><Data>test1</Data></Table><Table><Id>2</Id><Data>test2</Data></Table></NewDataSet>'
Then Prepare a procedure like below.
CREATE PROC [dbo].[UpdateMultipleRecords]
(
#XmlString VARCHAR(MAX)
)
AS
BEGIN
SET NOCOUNT ON;
CREATE TABLE #DATA
(
Id int,
Data varchar(50) NULL
)
DECLARE #DocHandle int
EXEC sp_xml_preparedocument #DocHandle OUTPUT, #XmlString
INSERT INTO #DATA
SELECT Id,Data
FROM OPENXML (#DocHandle, '/NewDataSet/Table',2)
WITH
(
Id int,
Data varchar(50)
)
EXEC sp_xml_removedocument #DocHandle
UPDATE [dbo].[Table1] SET DATA=D.Data
FROM [dbo].[Table1] T INNER JOIN #DATA D ON T.ID=D.Id
IF (SELECT OBJECT_ID('TEMPDB..#DATA')) IS NOT NULL DROP TABLE #DATA
END
And call the procedure as
[UpdateMultipleRecords] '<NewDataSet><Table><Id>1</Id><Data>Test1</Data></Table><Table><Id>2</Id><Data>Test2</Data></Table></NewDataSet>'
You need user-defined table types for this:
Try this:
-- test table
create table yourtable(id int not null, data [varchar](256) NULL)
GO
-- test type
CREATE TYPE [dbo].[usertype] AS TABLE(
[id] [int] not null,
[Data] [varchar](256) NULL
)
GO
-- test procedure
create procedure p_test
(
#tbl dbo.[usertype] READONLY
) as
BEGIN
UPDATE yourtable
SET data = t.data
FROM yourtable
JOIN
#tbl t
ON yourtable.id = t.id
END
go
-- test data
insert yourtable(id)
values(1),(2),(3)
go
Test of script:
declare #t [dbo].[usertype]
insert #t values(1,'hello'),(2,'world')
exec p_test #t
select * from yourtable
Result:
id data
1 hello
2 world
3 NULL
You can use another table with your values as a Source for the update
update t
set
Data = src.Data
from tableDestination t
inner join sourceTable src on
t.ID = src.ID