Avatar of HLRosenberger
HLRosenberger
Flag for United States of America asked on

Insert, Identity, All nulls

I have a table that uses an Identity column as the PK.   Is it possible using SQL to insert a record with NULLS for all fields except for the PK?  Now, I know I could specify one of the columns and use a value of NULL.  But I have multiple table liek this and I want a generic way to insert a record will all NULLS, without specifying the same of any of the columns.
Microsoft SQL ServerMicrosoft SQL Server 2005Microsoft SQL Server 2008

Avatar of undefined
Last Comment
HLRosenberger

8/22/2022 - Mon
Brian Crowe

I can't think of a way to create an empty record assuming you have made all of the other columns nullable.  Can you please expound on why you want to do this?
HLRosenberger

ASKER
Well, I'm using a code generator product.  It has Add and Edit pages.   I'm trying to make the Edit page work as both an Add and Edit page. To may it work like an "Add", I fake it out.  I Add a blank record and it Edits that record.  I want to make it generic.  All my table use an identity column as the PK.  So, I want to add a record with all NULL values (all are nullable) except for the PK.
HLRosenberger

ASKER
Is there a way to specify a column based on ordinal position?   That way I don't need to know the name of any column.
All of life is about relationships, and EE has made a viirtual community a real community. It lifts everyone's boat
William Peck
HLRosenberger

ASKER
a, I could do this:

select column_name from information_schema.columns where table_name = 'tch_attendance_week' and ordinal_position = 2

Then INSERT using the returned column name with a NULL value.
HLRosenberger

ASKER
This does not work, syntax wise, but you get the idea.  How could I make this work?

insert into [tds_grant_funding_request] (select column_name from information_schema.columns where table_name = 'tch_attendance_week' and ordinal_position = 2)
 values(null)
ASKER CERTIFIED SOLUTION
Neil Russell

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
HLRosenberger

ASKER
Ah yes, that would work.  but I dont want to depend on the number of columns.  I can up with a way, using a stored proc; ordinal_position 1 is always the identity PK.

CREATE PROCEDURE [dbo].[InsertNullRecord] @TABLE_NAME varchar(128) AS

DECLARE @COLUMN_NAME varchar(50);
DECLARE @SQL nvarchar(100);

BEGIN

DECLARE tempTable CURSOR FOR
      SELECT column_name from information_schema.columns WHERE table_name = @TABLE_NAME and ordinal_position = 2
      
OPEN tempTable
FETCH NEXT FROM tempTable INTO @COLUMN_NAME
CLOSE tempTable
DEALLOCATE tempTable      

SET @SQL = 'INSERT INTO ' + @TABLE_NAME + ' (' + @COLUMN_NAME + ') VALUES(NULL)'
EXEC sp_executesql   @SQL

END
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
Neil Russell

Not at a PC to play with right now but if you Copy this into SQL and run it, it should give you power you need to make the stored procedure as flexible as you need it.

DECLARE @Columns integer
DECLARE @sql varchar(max)
DECLARE @TABLE_NAME varchar(80)

SET @TABLE_NAME = 'myTableName'
SELECT @columns=COUNT(*) from information_schema.columns 
SELECT @columns

SET @sql = 'INSERT INTO ' + @table_name + ' VALUES('
WHILE (@columns >1)
BEGIN
	SET @sql = @sql + 'null'
	if(@columns = 2)
	BEGIN
		SET @sql = @sql + ')'
	END
	ELSE
	BEGIN
	        SET @sql = @sql + ', '
	END
	SET @columns = @columns - 1
END
SELECT @sql

Open in new window

Neil Russell

Or the more efficient method of....

DECLARE @Columns integer
DECLARE @iloop integer
DECLARE @sql varchar(max)
DECLARE @TABLE_NAME varchar(80)

SET @TABLE_NAME = 'myTableName'
SELECT @columns=COUNT(*) from information_schema.columns 
SELECT @columns

SET @sql = 'INSERT INTO ' + @table_name + ' VALUES('  + REPLICATE('null, ',@columns-1) + ')'
SELECT @sql

Open in new window

typetoit

If all of the fields allow NULLs, then you only need to insert one value into the table in order to get a new record.
Your help has saved me hundreds of hours of internet surfing.
fblack61
Neil Russell

Yes but you need a column name.  My solution was to allow a quick insert with no column names and any number of columns thats all.
Olaf Doschke

There is the less known DEFAULT keyword, eg

declare @mytable as table(id int identity, mytext varchar(20) default 'hello, world' null, mydate date default getdate() null, mybit bit NULL);

insert into @mytable values (default, default, default)

select * from @mytable

Open in new window


You'll need to know the nnumber of columns for this, but it will have another advantage, you'll not only get NULLs in all columns, but default values, where they are defined, The sample column mybit has no default value, then the type default is used - NULL in case of a nullable column.

Bye, Olaf.
Scott Pletcher

Make the default NULL for all the non-PK columns.

Then you can do this:

INSERT INTO table_name DEFAULT VALUES
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
Olaf Doschke

Thanks for sharing, Scott. That's even better of course!
HLRosenberger

ASKER
thanks