2014-11-04 45 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