create procedure sp_login
as
insert into tbluserlog (_action,date_added,userId) values
('login',getdate(),(select tbluser.userid from tbluser inner join tbluserlog
on tbluser.userid=tbluserlog.userid))
这可能吗?如果没有请建议
答案 0 :(得分:3)
可以选择INSERT语句的查询部分。你可以像这样使用它。
INSERT into tbluserlog (_action,date_added,userId)
SELECT
'login'
,getdate()
,tbluser.userid
FROM tbluser
INNER JOIN tbluserlog on tbluser.userid=tbluserlog.userid
答案 1 :(得分:0)
或者您可以将UserID分配给变量,如下所示 -
CREATE PROCEDURE sp_login
AS
BEGIN
DECLARE @UserID INT = (SELECT tbluser.userid
FROM tbluser
JOIN tbluserlog
ON tbluser.userid = tbluserlog.userid)
INSERT tbluserlog (_action,date_added,userId)
VALUES ('login', GETDATE(), @UserID)
END
答案 2 :(得分:0)
是的,但语法不是“值”,而是
insert into tbluserlog(action, date_added, userId)
select 'login',
getdate(),
tbluser.userid
from tbluser
inner join tbluserlog on tbluser.userid = tbluserlog.userid
...但我真的不认为这是你想要的。这将为每个现有的tbluserlog行插入一个新的tbluserlog行。
我猜你想把userid作为参数传递,即
create procedure sp_login
@userid int
as
insert into tbluserlog(action, date_added, userId)
select 'login',
getdate(),
@userid
答案 3 :(得分:0)
我不会在sp_
前加上它们为内置程序保留它们的最佳做法,如果删除login
,最好不要使用sp_
之类的保留字。你可以这样做:
CREATE PROCEDURE [login]
AS
INSERT INTO tbluserlog
(
_action,
date_added,
userId
)
SELECT
'login',
GETDATE(),
tbluser.userid
FROM tbluser
INNER JOIN tbluserlog ON tbluser.userid = tbluserlog.userid