Dan*_*any 2 database sql-server stored-procedures sql-server-2008
我知道,这是一个非常基本的问题,但我在重新学习sql存储过程的过程中,这就是背后的问题:
I was practicing with some simple delete/insert routines, when encountered with this: if I ONLY insert records:
all is ok, then I call a delete procedure, passing the colum_ID to identify the record and delete it.
This is done correctly and all is OK
Then I call a insert procedure once again and the insert happens like this:
And of i delete lets say records 2 and 3, and call the insert procedure for 2 new records, ir will "fill out" those positions and the third record will be at the bottom, where it should be.
The stored procedures are very basic: Insert
USE [Test]
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER PROCEDURE [dbo].[InsertData]
@name varchar(50),
@lastname varchar(50)
AS
BEGIN
SET NOCOUNT ON;
insert into dbo.names (name,last_name)
values(@name,@lastname)
select * from dbo.names
END
Run Code Online (Sandbox Code Playgroud)
Delete
USE [Test]
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER PROCEDURE [dbo].[DeleteData]
@id_record int
AS
BEGIN
SET NOCOUNT ON;
delete from dbo.names where id_record = @id_record
SELECT * from dbo.names
END
Run Code Online (Sandbox Code Playgroud)
So, what is causing this behavior, the table is very basic, but is compliant with Pkeys, not nulls, indexes, etc.
The order that rows are returned from a select statement is arbitrary, unless you use an 'order by' clause.
In practice, the order will be determined by the particular implementation that your DBMS uses, but you should not assume that the order is stable.