2014-11-04 50 views
-1

插入特定的列我有Student考勤表,並具有類似如何在SQL Server

st_id | StudentName | month | year | day1 to day30 

列和,asp.net我使用。當我點擊Present按鈕時,數據庫的cols應該插入st_id,StudentName,month,year的值,而一天應該是systemdate。日期應該在白天列在SQL輸入和剩餘的應該是空值

suppose if i enter the date today ie, 4 data will enter in day4 col like present 

http://i.stack.imgur.com/K7LwU.png

+0

這是格式。 '插入表名(列名)的值(值1)' – 2014-11-04 11:41:24

+0

動態它應該輸入當天的日期,我們不應該一次又一次的修改 – Meena 2014-11-04 11:43:57

+0

我不想在列中獲取日期我想插入當前列列出現或缺席像我有進入 – Meena 2014-11-04 11:47:31

回答

2

嘗試這樣

DECLARE @st_id int=1 
DECLARE @StudentName varchar(50)='dinesh' 
DECLARE @month varchar(50)='jan' 
DECLARE @year int=2014 

DECLARE @SQL nvarchar(MAX)= 'insert into 
(st_id, StudentName, month, year,day'+CAST(datepart(day,GETDATE()) as 
varchar(10))+') values 
('+CAST(@st_id as VARCHAR(MAX))+','''[email protected]+''','''+ 
CAST(@month as VARCHAR(MAX)) 
+''','+CAST(@year as VARCHAR(10))+',''present'')' 
print @SQL 

使用步驟

Create procedure sp_insertStudents 
@st_id int, 
@StudentName varchar(50), 
@month varchar(50), 
@year int 
as 
BEGIN 


DECLARE @SQL nvarchar(MAX)= 'insert into 
(st_id, StudentName, month, year,day'+CAST(datepart(day,GETDATE()) as 
varchar(10))+') values 
('+CAST(@st_id as VARCHAR(MAX))+','''[email protected]+''','''+ 
CAST(@month as VARCHAR(MAX)) 
+''','+CAST(@year as VARCHAR(10))+',''present'')' 
print @SQL 
EXEC(@SQL) 
END 

輸出

insert into (st_id, StudentName, month, year,day4) values 
(1,'dinesh','jan',2014,'present') 
+0

@MeenaKonakondla現在更新 – 2014-11-04 12:44:47

+0

@MeenaKonakondla,如果你認爲這是有幫助的將其標記爲答案 – 2014-11-04 18:25:19

+0

@ Ganesh_Devlekar的答案我question.how可以在StoredProcedure的 – Meena 2014-11-05 04:47:14