且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何在SQL表中动态插入列值

更新时间:2023-01-22 16:31:34

^ ]如果您按创建顺序为所有列提供值,则只能在不指定列名的情况下使用该语句.

如果您不想指定列,但只想插入某些列,则必须对其他列使用默认值.

在第一种情况下,您使用
The SQL INSERT[^] statement can only be used without specifying the column names if you supply a value for all the column in order they were created.

If you do not want to specify the columns but also want to insert only certain columns you will have to use default value for the other columns.

In the first case you use
INSERT TAB1 (<No value>, <Name value>, <Subject value>, DEFAULT, DEFAULT)


在第二种情况下,您使用


In the second case you use

INSERT TAB1 (DEFAULT, DEFAULT, DEFAULT, <Salary value>, <Dept value>)



仅当您为所有列指定了默认值并且约束条件允许插入值时,此方法才有效.
在您的情况下,只有在没有为表定义主键的情况下,这才起作用.



This only works if you have specified default values for all the columns and the constrains allow the insertion of the values.
In your case this would only work if you have no primary key defined for the table.


您好,亲爱的.........
首次使用插入到.
和第二次用户更新查询.

插入TAb1
(NO,name)values(12345,''aa'')


更新tabl dept =,薪水=否= 12345
hello dear.........
Use Insert into for first time.
and second time user update query.

Inset into TAb1
(NO, name) values(12345,''aa'')


update tabl dept = , salary = where no = 12345




您必须在更新薪金和部门列的同时更新该行

在这里,我根据您的要求提供一些想法,请检查一次

Hi,

you''ve to update that row while updating salary and dept columns

Here I''m providing some idea on your requirement check this once

  create procedure AddData
(
 @cmdtype nvarchar(50),
 @Name nvarchar(50),
 @sub nvarchar(50),
 @no int=NULL,
 @sal money=NULL,
 @dept nvarchar(50)=NULL
)
as begin
 if(@cmdtype=="I")
{
   insert into tablename values (@Name,@sub,@sal,@dept);
}
else
{
  if(@no not null)
{
  update tablename set sal=@sal,dept=@dept where @no
}
}
end



id是自动生成的列.


执行过程是



Here id is auto generated column .


execution procedure is

exec AddData 'I','murali','SQL'

exec AddData 'U','murali','SQL',1154,30000,'Developement'



在上面的代码中,第一行用于插入第二行以更新sal和dept

***的



in the above code first line for inserting second line for updating sal and dept

All the Best