December 17, 2013 at 11:14 am
Can anyone help me why I get the error, while executing below query after creating the table. Why I can't able to insert default timestamp value in the table while doing insert statement. Please provide your info. Thanks.
create table X
(
DBName varchar(20),
NumberOfConnectionstoDB INT,
Loginame VARCHAR(50),
HostName VARCHAR(50),
[program_name] varchar (200),
cmd varchar(200),
Logintime datetime default CURRENT_TIMESTAMP
)
insert into X
SELECT DB_NAME(dbid) as DBName,
COUNT(dbid) as NumberOfConnectionstoDB,
loginame as LoginName,
hostname,[program_name],cmd
FROM
sys.sysprocesses
WHERE
DB_NAME(dbid) in ('master','msdb')
GROUP BY
dbid, loginame,hostname,[Program_name],cmd
December 17, 2013 at 11:39 am
seven columns in the table, only six in the query;
your table has the column name Logintime , but your query does not.
the implied column list, for when you say INSERT INTO X always gets all the columns for the target table.
you either need to name the columns to insert into explicitly, or have the query return getdate() AS Logintime
Lowell
December 17, 2013 at 12:42 pm
Lowell,
Thanks, for some reason, i just got diverted. Re queried BY ADDING getdate() into my select query and was able to get my result. THANKS
December 17, 2013 at 2:31 pm
I have always made it a practice that when I'm inserting into or selecting out of a table I always specify the columns. In your case when you are performing your insert, you are not specifying the columns you are populating.
The syntax I use is:
insert into <some table> (col1, col2, col3...coln)
select col1, col2, col3...coln from <some other table>
where <some where clause>
There are many reasons for doing this. If your tables change by adding additional columns and you are not specifying the column list then your code will break.
Just a word to the wise.
Kurt
Kurt W. Zimmerman
SR DBA
Lefrak Organization
New York, NY
http://www.linkedin.com/in/kurtwzimmerman
December 18, 2013 at 1:18 pm
Kurt W. Zimmerman (12/17/2013)
I have always made it a practice that when I'm inserting into or selecting out of a table I always specify the columns. In your case when you are performing your insert, you are not specifying the columns you are populating.The syntax I use is:
insert into <some table> (col1, col2, col3...coln)
select col1, col2, col3...coln from <some other table>
where <some where clause>
There are many reasons for doing this. If your tables change by adding additional columns and you are not specifying the column list then your code will break.
Just a word to the wise.
Kurt
+1000!
--Jeff Moden
Change is inevitable... Change for the better is not.
Viewing 5 posts - 1 through 4 (of 4 total)
You must be logged in to reply to this topic. Login to reply