我昨天发了一个问题,这是相关的。我有一个名为People_Master的表
public class MainActivity extends Activity implements AdapterView.OnItemSelectedListener {
private Button validateUser;
private Spinner userTypeSpinner;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
userTypeSpinner=(Spinner)findViewById(R.id.userTypeSpinner);
validateUser= (Button) findViewById(R.id.userValidate);
validateUser.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Toast.makeText(getApplicationContext(), "Button is Selected", Toast.LENGTH_SHORT);
}
});
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this,
R.array.userTypeArray, android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
userTypeSpinner.setAdapter(adapter);
userTypeSpinner.setOnItemSelectedListener(this);
}
@Override
public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
String item = adapterView.getItemAtPosition(i).toString();
Toast.makeText(adapterView.getContext(), "Selected: " + item, Toast.LENGTH_SHORT).show();
}
@Override
public void onNothingSelected(AdapterView<?> adapterView) {
}
}
我创建了另一个名为People_Delta的表
CREATE TABLE [dbo].[People_Master](
[ID] [int] IDENTITY(900001,1) NOT NULL,
[Email] [nvarchar](50) NULL,
[FirstName] [nvarchar](50) NULL,
[uac] [int] NULL,
[department] [nvarchar](50) NULL,
CONSTRAINT [PK_People_Master] PRIMARY KEY CLUSTERED
(
[ID] ASC
)
请注意,我在People_Delta中还有一个名为&#34; change_type &#34;
的列现在我正在尝试为People_Master编写更新触发器,以便在更新发生时,它应该将整行写入People_Delta并将change_type设置为&#34;修改&#34;。
这是我写过的触发器(我不擅长SQL。大声笑!)
CREATE TABLE [dbo].[People_Delta](
[ID] [int] NOT NULL,
[Email] [nvarchar](50) NULL,
[FirstName] [nvarchar](50) NULL,
[uac] [int] NULL,
[Department] [nvarchar](50) NULL,
[change_type] [nvarchar](10) NULL
) ON [PRIMARY]
但是,当我执行此触发器时,我收到以下错误:
CREATE TRIGGER [dbo].[TR_Update]
ON [dbo].[People_Master]
AFTER Update
AS
BEGIN
-- SET NOCOUNT ON added to prevent extra result sets from
-- interfering with SELECT statements.
SET NOCOUNT ON;
Declare @RowCount int
Declare @ID int
--Create a temp table to hold the inserted data.
Declare @TempTable As Table
(
[ID] [int],
[Email] [nvarchar](50) ,
[FirstName] [nvarchar](50),
[uac] [int] ,
[department] [nvarchar](50)
)
Insert Into @TempTable (ID,Email,FirstName,uac,department)
select
ID,
Email,
FirstName,
uac,
department
from
inserted
select @ID = ID from inserted
--check if the row for this ID doesnt exist already.
Select @RowCount=COUNT(*) from People_Delta where People_Delta.ID = @ID and People_Delta.change_type = 'Modify';
if(@RowCount = 0)
Begin
Insert into People_Delta (ID,Email,FirstName,uac,Department,change_type)
values (@TempTable.ID,@TempTable.Email,@TempTable.FirstName,@TempTable.uac,@TempTable.department,'Modify');
END
END
GO
我做错了什么?请帮忙! GT
答案 0 :(得分:0)
尝试此触发器。
更改了此插入查询
Insert into People_Delta (ID,Email,FirstName,uac,Department,change_type)
values (@TempTable.ID,@TempTable.Email,@TempTable.FirstName,@TempTable.uac,
@TempTable.department,'Modify');
到
Insert into People_Delta (ID,Email,FirstName,uac,Department,change_type)
select ID, Email,FirstName, uac, department, 'Modify' from
inserted
更新了触发器:
CREATE TRIGGER [dbo].[TR_Update]
ON [dbo].[People_Master]
AFTER Update
AS
BEGIN
-- SET NOCOUNT ON added to prevent extra result sets from
-- interfering with SELECT statements.
SET NOCOUNT ON;
Declare @ID int
select @ID = ID from inserted
If NOT EXISTS(Select People_Delta.ID from People_Delta where People_Delta.ID = @ID
and People_Delta.change_type = 'Modify')
BEGIN
Insert into People_Delta (ID,Email,FirstName,uac,Department,change_type)
select ID, Email,FirstName, uac, department, 'Modify' from
inserted
END
END