我正在尝试将详细信息插入到已成功完成的Emp Table中。 但我的要求是在下次再次尝试插入细节时首先截断表。 因此我写了truncate语句,它不断地逐行截断。
结果是表格中出现的文件的最后一行。
有人可以帮我修改程序吗?
ALTER PROCEDURE [dbo].[Insert_Deatils]
@Name(100),
@DESCRIPTION(100),
@Address(100)
AS
BEGIN
SET NOCOUNT ON;
declare @CountNew varchar(100)
set @CountNew=(select count(*) from [dbo].[Emp_Details] )
if @CountNew>=1
truncate table [dbo].[Emp_Details]
else
INSERT INTO [dbo].[Emp_Details]
([Name],
[DESCRIPTION],
[Address]
)
VALUES
(
@Name,
@DESCRIPTION,
@Address
)
END
Begin
if exists(select 1 from [dbo].[Emp_Details] where Name='Raul')
Delete from [dbo].[Emp_Details] where AIRCRAFT_REGISTRATION='Raul'
End
答案 0 :(得分:0)
要考虑的一些事项......
TRUNCATE
删除表格中的所有行,并重置所有IDENTITY
列。它是一个未记录在事务日志中的操作,因此非常快。
DELETE
将删除行,但会记录操作。此外,任何IDENTITY列都不会重新种植。
如果您只是想在开始运行proc时清空表,那么就足够了:
TRUNCAE TABLE [dbo].[Emp_Details]
INSERT INTO [dbo].[Emp_Details]
([Name], [DESCRIPTION], [Address])
VALUES
(@Name, @DESCRIPTION, @Address)
只有在您回复DELETE
操作的结果之前需要删除特定结果时,才应使用最后的INSERT
。如果您的目标是在再次触发proc时清空表,则截断将处理该问题。
还有一些关于我最后几行的内容。
Begin
if exists(select 1 from [dbo].[Emp_Details] where Name='Raul')
Delete from [dbo].[Emp_Details] where AIRCRAFT_REGISTRATION='Raul'
End
你的意思是这样做吗?
if exists(select 1 from [dbo].[Emp_Details] where Name='Raul')
Begin
Delete from [dbo].[Emp_Details] where AIRCRAFT_REGISTRATION='Raul'
End
(同样,如果您的唯一目标是清空表格,则没有必要)
另外,您提示有错误,但未发布错误。
答案 1 :(得分:0)
Stored Procedure
中的修正很少,请查看这是否符合您的要求。
好的,现在得到了你的要求,
在TRUNCATE
或Loop
之前调用Bulk Insert
语句。这可确保Table
为空以插入新记录。
TRUNCATE TABLE [dbo].[Emp_Details]
然后将Procedure
调用Table
ALTER PROCEDURE [dbo].[Insert_Deatils]
@Name VARCHAR(100),
@DESCRIPTION VARCHAR(100),
@Address VARCHAR(100)
AS
BEGIN
SET NOCOUNT ON;
INSERT INTO [dbo].[Emp_Details]
([Name],[DESCRIPTION],[Address])
VALUES
(@Name,@DESCRIPTION,@Address)
BEGIN
IF EXISTS(SELECT 1 FROM [dbo].[Emp_Details] WHERE NAME = 'Raul')
DELETE FROM [dbo].[Emp_Details] WHERE AIRCRAFT_REGISTRATION = 'Raul'
END
END
Table-Valued Parameters
或者您可以使用CREATE TYPE Emp_Details AS TABLE
( [Name] VARCHAR(100)
, [Description] VARCHAR(100)
, [Address] VARCHAR(100));
GO
CREATE PROCEDURE [dbo].[Insert_Deatils]
@Tmp Emp_Details READONLY
AS
BEGIN
SET NOCOUNT ON
TRUNCATE TABLE [dbo].[Emp_Details]
INSERT INTO [dbo].[Emp_Details]
([Name]
,[Description]
,[Address])
SELECT [Name]
,[Description]
,[Address]
FROM @Tmp;
BEGIN
IF EXISTS(SELECT 1 FROM [dbo].[Emp_Details] WHERE NAME = 'Raul')
DELETE FROM [dbo].[Emp_Details] WHERE AIRCRAFT_REGISTRATION = 'Raul'
END
END
GO
/* Declare a variable that references the type. */
DECLARE @Emp AS Emp_Details;
/* Add data to the table variable. */
INSERT INTO @Emp ([Name], [Description], [Address])
SELECT [Name], [Description], [Address]
FROM Emp_Details1;
/* Pass the table variable data to a stored procedure. */
EXEC [dbo].[Insert_Deatils] @Emp;
GO
一次性传递所有行。
USE <Database>
GO
CREATE TYPE Emp_Details AS TABLE
( [Name] VARCHAR(100)
, [Description] VARCHAR(100)
, [Address] VARCHAR(100));
GO
CREATE TABLE Emp_Details
( [Name] VARCHAR(100)
, [Description] VARCHAR(100)
, [Address] VARCHAR(100));
GO
CREATE TABLE Emp_Details1
( [Name] VARCHAR(100)
, [Description] VARCHAR(100)
, [Address] VARCHAR(100));
GO
INSERT INTO Emp_Details1 VALUES ('John','Test','123')
INSERT INTO Emp_Details1 VALUES ('John1','Test1','1234')
INSERT INTO Emp_Details1 VALUES ('John2','Test2','1235')
GO
SELECT * FROM Emp_Details
/* Declare a variable that references the type. */
DECLARE @Emp AS Emp_Details;
/* Add data to the table variable. */
INSERT INTO @Emp ([Name], [Description], [Address])
SELECT [Name], [Description], [Address]
FROM Emp_Details1;
/* Pass the table variable data to a stored procedure. */
EXEC [dbo].[Insert_Deatils] @Emp;
GO
SELECT * FROM Emp_Details
您可以查看Table-Valued Parameters以获取有关相同
的更多信息在我的结尾尝试了下面的示例,似乎工作正常
from sklearn import svm
from sklearn.feature_extraction.text import TfidfVectorizer
label = ['organisasi','organisasi','organisasi','organisasi','organisasi','lokasi','lokasi','lokasi','lokasi','lokasi']
text = ['Partai Anamat Nasional','Persatuan Sepak Bola', 'Himpunan Mahasiswa','Organisasi Sosial','Masyarakat Peduli','Malioboro','Candi Borobudur','Taman Pintar','Museum Sejarah','Monumen Mandala']
vectorizer = TfidfVectorizer(min_df=1)
X = vectorizer.fit_transform(text)
y = label
klasifikasi = svm.SVC()
klasifikasi = klasifikasi.fit(X,y) #training
test_text = ['Partai Perjuangan']
test_vector = vectorizer.fit_transform(test_text)
prediksi = klasifikasi.predict([test_vector]) #test
print(prediksi)