2016-12-24 71 views
-1

我在哪裏可以把下面的語句這getdate()電話:哪裏可以放這個getdate()調用?

CREATE PROCEDURE sp_test 
    (@email nvarchar(50), @password nvarchar(50)) 
AS 
    SELECT employee_id, position 
    FROM tbl_employee_tbl 
    WHERE email = @email AND login_password = @password 

    INSERT INTO tbl_log_activity(employee_id, sign_in_time) 
     SELECT 
      employee_id 
     FROM 
      tbl_employee_tbl,getdate() 
     WHERE 
      email = @email 
+2

旁註:你應該** **不使用'sp_'前綴爲您的存儲過程。微軟已經保留了這個前綴以供自己使用(參見*命名存儲過程*)](http://msdn.microsoft.com/en-us/library/ms190669%28v=sql.105%29.aspx),以及你將來有可能冒着名字衝突的風險。 [這對你的存儲過程性能也是不利的](http://www.sqlperformance.com/2012/10/t-sql-queries/sp_prefix)。最好只是簡單地避免使用'sp_'並將其他內容用作前綴 - 或者根本沒有前綴! –

回答

3

更改第二條語句是這樣的:

INSERT INTO tbl_log_activity(employee_id,sign_in_time) 
SELECT employee_id, Getdate() 
FROM tbl_employee_tbl 
WHERE [email protected] 
+0

** ALTER過程[DBO]。[sp_test對它](@電子郵件爲nvarchar(50),@密碼爲nvarchar(50))作爲 選擇 EMPLOYEE_ID,從tbl_employee_tbl位置電子郵件= @電子郵件和login_password = @password 插入到tbl_log_activity(employee_id,sign_in_time)選擇employee_id,從tbl_employee_tbl中選擇FORMAT(GETDATE(),'dd/mm/yyyy,hh:mm:ss'),其中email = @ email ** –

+0

我想更改日期格式 –

1
create procedure 
sp_test(@email nvarchar(50),@password nvarchar(50)) 
as 
select 
    employee_id, 
    position 
from 
    tbl_employee_tbl 
where 
    [email protected] and login_password= @password 

insert into 
    tbl_log_activity(employee_id,sign_in_time) 
    select 
     employee_id 
     ,getdate() 
    from 
     tbl_employee_tbl 
    where [email protected] 
+0

ALTER過程[DBO]。[sp_test對它](@電子郵件爲nvarchar(50),@密碼爲nvarchar(50))作爲 選擇 EMPLOYEE_ID,從tbl_employee_tbl位置電子郵件= @電子郵件和login_password = @password 插入到tbl_log_activity(EMPLOYEE_ID, sign_in_time)選擇employee_id,選擇FORMAT(GETDATE(),'dd/mm/yyyy,hh:mm:ss')as date from tbl_employee_tbl where email = @ email –

+0

我想改變dae格式..在這個聲明中 –

+0

@Nauman Arain'sign_in_time'的數據類型是什麼? –

0

你可以試試這個:

FORMAT()函數在SQL Server 2008中不起作用,所以你可以去找CONVERT()

ALTER procedure [dbo].[sp_test] 
(
@email nvarchar(50), 
@password nvarchar(50) 
) 
as 

select employee_id,position 
from tbl_employee_tbl 
where [email protected] and login_password= @password 

insert into tbl_log_activity(employee_id,sign_in_time) 
select employee_id,convert(varchar(25), GETDATE(),131) as date 
from tbl_employee_tbl 
where [email protected] 

注:但我會建議你不要儲存日期與數據庫中的任何格式。將其存儲爲日期時間,並且只要您希望訪問該值,就可以在選擇查詢中對其進行格式化並根據我們的需要進行顯示。

如果你想在數據庫中存儲具有特定格式的日期,請確保你有作爲varchar(25)的sign_in_time列。

0

改變這樣的代碼

CREATE PROCEDURE sp_test 
(@email nvarchar(50), @password nvarchar(50)) 
AS 
SELECT employee_id, position 
FROM tbl_employee_tbl 
WHERE email = @email AND login_password = @password 

INSERT INTO tbl_log_activity(employee_id, sign_in_time) 
    SELECT 
     employee_id ,getdate() 
    FROM 
     tbl_employee_tbl 
    WHERE 
     email = @email