Wednesday, March 7, 2012
ALTER COLUMN?
NULLs. I want to make it non-nullable and give it a DEFAULT value of 0. I've
looked up ALTER TABLE in BOL but I don't seem to see what the syntax is to
accomplish this. Is it even possible? TIA!You can do someting like this
CREATE TABLE TESTDEFAULTS (
ID INT)
INSERT INTO TESTDEFAULTS
VALUES (1)
SELECT *
FROM TESTDEFAULTS
ALTER TABLE TESTDEFAULTS ALTER COLUMN ID INT NOT NULL
ALTER TABLE TestDefaultS ADD CONSTRAINT IDNotNull DEFAULT (0) FOR [ID]
INSERT INTO TESTDEFAULTS
DEFAULT VALUES
SELECT *
FROM TESTDEFAULTS
--This will give an error now
INSERT INTO TestDefaultS VALUES (NULL)
DROP TABLE TESTDEFAULTS
Denis the SQL Menace
http://sqlservercode.blogspot.com/|||Thanks! At first I got an error but once I UPDATEd the column to 0 WHERE it
IS NULL, voila! Again, thank you!
"SQL" <denis.gobo@.gmail.com> wrote in message
news:1147462107.839063.66230@.d71g2000cwd.googlegroups.com...
> You can do someting like this
> CREATE TABLE TESTDEFAULTS (
> ID INT)
> INSERT INTO TESTDEFAULTS
> VALUES (1)
> SELECT *
> FROM TESTDEFAULTS
> ALTER TABLE TESTDEFAULTS ALTER COLUMN ID INT NOT NULL
> ALTER TABLE TestDefaultS ADD CONSTRAINT IDNotNull DEFAULT (0) FOR [ID]
> INSERT INTO TESTDEFAULTS
> DEFAULT VALUES
>
> SELECT *
> FROM TESTDEFAULTS
> --This will give an error now
> INSERT INTO TestDefaultS VALUES (NULL)
> DROP TABLE TESTDEFAULTS
>
> Denis the SQL Menace
> http://sqlservercode.blogspot.com/
>
Friday, February 24, 2012
AllowNulls overrides default?
a default of getdate(), will this column value always be NULL if nothing is
inserted into it upon record insertion of other columns?
It seems to be. Must I delete that column if I want to not allow NULLs?
Thanks,
BrettAllowing null will not override default. See below:
CREATE TABLE #t (c1 int, c2 datetime null default getdate())
INSERT INTO #t(c1, c2) values(1, null)
INSERT INTO #t(c1) values(1)
INSERT INTO #t(c1, c2) values(1, DEFAULT)
SELECT * FROM #t
Tibor Karaszi, SQL Server MVP
http://www.karaszi.com/sqlserver/default.asp
http://www.solidqualitylearning.com/
http://www.sqlug.se/
"Brett" <no@.spam.net> wrote in message news:%23fgmZWIKFHA.2880@.TK2MSFTNGP09.phx.gbl...[colo
r=darkred]
> If I have a table column type smalldate checked to allow NULLs and also ha
ve a default of
> getdate(), will this column value always be NULL if nothing is inserted in
to it upon record
> insertion of other columns?
> It seems to be. Must I delete that column if I want to not allow NULLs?
> Thanks,
> Brett
>[/color]|||Well, I always get NULLs in that column rather than the default.
Brett
"Tibor Karaszi" <tibor_please.no.email_karaszi@.hotmail.nomail.com> wrote in
message news:e6Bl3cIKFHA.3500@.TK2MSFTNGP14.phx.gbl...
> Allowing null will not override default. See below:
> CREATE TABLE #t (c1 int, c2 datetime null default getdate())
> INSERT INTO #t(c1, c2) values(1, null)
> INSERT INTO #t(c1) values(1)
> INSERT INTO #t(c1, c2) values(1, DEFAULT)
> SELECT * FROM #t
>
> --
> Tibor Karaszi, SQL Server MVP
> http://www.karaszi.com/sqlserver/default.asp
> http://www.solidqualitylearning.com/
> http://www.sqlug.se/
>
> "Brett" <no@.spam.net> wrote in message
> news:%23fgmZWIKFHA.2880@.TK2MSFTNGP09.phx.gbl...
>|||Did you run my script?
What application are you using?
Did you Profiler trace the application to see the INSERT statement?
My guess is that the applications specifies NULL for the column in the INSER
T statement.
Tibor Karaszi, SQL Server MVP
http://www.karaszi.com/sqlserver/default.asp
http://www.solidqualitylearning.com/
http://www.sqlug.se/
"Brett" <no@.spam.net> wrote in message news:uo1DmpIKFHA.3788@.tk2msftngp13.phx.gbl...darkred">
> Well, I always get NULLs in that column rather than the default.
> Brett
> "Tibor Karaszi" <tibor_please.no.email_karaszi@.hotmail.nomail.com> wrote i
n message
> news:e6Bl3cIKFHA.3500@.TK2MSFTNGP14.phx.gbl...
>|||Brett wrote:
> Well, I always get NULLs in that column rather than the default.
> Brett
>
Please post your complete insert statement.
David Gugick
Imceda Software
www.imceda.com|||"David Gugick" <davidg-nospam@.imceda.com> wrote in message
news:u5elsKKKFHA.2784@.TK2MSFTNGP09.phx.gbl...
> Brett wrote:
> Please post your complete insert statement.
> --
> David Gugick
> Imceda Software
> www.imceda.com
I think this is occuring because I added this column after the table was
created. I had to allow NULLs in order to add the column. Let me research
a little more and I will post my results.
Thanks,
Brett|||Brett wrote:
> "David Gugick" <davidg-nospam@.imceda.com> wrote in message
> news:u5elsKKKFHA.2784@.TK2MSFTNGP09.phx.gbl...
> I think this is occuring because I added this column after the table
> was created. I had to allow NULLs in order to add the column. Let
> me research a little more and I will post my results.
> Thanks,
> Brett
I just tested with an added column and I get the default value when
inserted. We'll await your post.
David Gugick
Imceda Software
www.imceda.com|||"David Gugick" <davidg-nospam@.imceda.com> wrote in message
news:ONOXbmKKFHA.3356@.TK2MSFTNGP12.phx.gbl...
> Brett wrote:
> I just tested with an added column and I get the default value when
> inserted. We'll await your post.
>
But if you create a table, add four records then add the new column with a
default and allow nulls, what values does it have? I tried this and the
results are as follows:
Adding new records to the table I mention in this post does get the default.
Those in the table before I added this new column have NULLs. I couldn't
add the new column without allowing NULLs because of the previous records.
Thanks,
Brett|||Brett wrote:
> But if you create a table, add four records then add the new column
> with a default and allow nulls, what values does it have? I tried
> this and the results are as follows:
> Adding new records to the table I mention in this post does get the
> default. Those in the table before I added this new column have
> NULLs. I couldn't add the new column without allowing NULLs because
> of the previous records.
Ok, I think what you are saying is that the existing rows that were in
the table before you added the new column do not pick up the default.
That's expected behavior. Defaults are only applied on inserts when the
column is left out of the insert. There is no way for SQL Server to know
whether those NULLs in the existing columns were intentionally put
there. You'll need to run an update on the table and change the values
you need changed to the default.
David Gugick
Imceda Software
www.imceda.com
Thursday, February 16, 2012
Allow NULLS with the lookup transformation?
Hi,
I just wanted to know if there is any way to Allow Null values while doing a lookup on a table in SSIS.
Let me elaborate the situation...
I have a flat file source that has a field called 'code'. I want to lookup in a code table to see if the code in the file is a valid code but the flat file may contain a NULL value as a 'code' (i.e. zero length string which treated as NULL by my package).
My problem is, the SSIS package tries to search for the NULL in the table and the lookup fails and an error is logged as per the business logic but actually NULL is also an acceptable value and the error should not be logged.
I tried inserting a NULL value in the lookup column but that doesn't work. I am not sure but I think I have read somewhere that two null values cannot be compared for equality. I cannot use conditional split to check the null value because I have to use a large number of lookups and a conditional split everywhere will mess up the things.
Is there any way to solve this problem?
try doing an Ignore Failure in the lookup component.|||Ignore failure will ignore this error for all the values coming in the feed.
I want to ignore only in the case of NULL values. and for values other than NULL (and invalid), it should be redirected to the error output where the error will be logged.
|||Use a conditional split right before the lookup to redirect NULL values. Then after the lookup, you can join the two flows back together.|||Replace the NULL value with an empty string prior to the lookup, add an empty string to your lookup source, then replace the empty string with NULL after the lookup. Or use some other value to represent NULLs, if an empty string won't work. A little kludgy, but it works.|||yes... It will be a very complex solution. I would probably do that only.
I am just looking for a graceful way to handle this scenario. Please notify me if you come across some links adressing similar issues.
Sunday, February 12, 2012
ALL STORED PROCS RETURNING NOTHING BUT NULLS IN OUTPUT PARAMETERS
know what. Suddenly, all OUTPUT parameters from ANY SPROC return NULL.
The funny thing is, the values are in the variables within the SPROCS
(see the PRINT results on the SPROC example below) The behavior is the
same in Query Analyzer as in ADO.Net.
Any help would be GREATLY appreciated.
Thanks,
Brad
Here is some detail:
Stored Proc Definition
=====================================
ALTER PROC DBO.USP_GET_POOL_INFO
@.POOLID INT
,@.PRIVATE CHAR(1) OUTPUT
,@.PLAYERLIMIT NVARCHAR(33) OUTPUT
,@.PLAYERCOUNT INT OUTPUT
AS
SELECT
@.PRIVATE = private
,@.PLAYERLIMIT = CASE WHEN p.player_limit = 0 OR p.player_limit IS
NULL THEN 'No Limit' ELSE CAST(p.player_limit AS NVARCHAR(33)) END
,@.PLAYERCOUNT = ISNULL(pc.player_count,'')
from
tPools p
LEFT OUTER JOIN
(
SELECT
POOLID,
COUNT(*) player_count
FROM
tProfilePool
GROUP BY
POOLID
) pc
ON
p.pool_id = pc.poolid
where
pool_id = @.POOLID
PRINT @.PRIVATE
PRINT @.PLAYERLIMIT
PRINT @.PLAYERCOUNT
========================================
====
Stored Proc Execution:
========================================
====
DECLARE @.PLAYERLIMIT NVARCHAR(33)
DECLARE @.PLAYERCOUNT INT
DECLARE @.PRIVATE CHAR(1)
EXEC DBO.USP_GET_POOL_INFO 2, @.PRIVATE, @.PLAYERLIMIT, @.PLAYERCOUNT
SELeCT @.PRIVATE, @.PLAYERLIMIT, @.PLAYERCOUNT
Result:
NULL NULL NULL
Messages Result (from the PRINT command):
Y
No Limit
1
========================================
Raw TSQL Approach:
========================================
====
DECLARE @.PRIVATE CHAR(1)
DECLARE @.PLAYERLIMIT VARCHAR(33)
DECLARE @.PLAYERCOUNT INT
SELECT
@.PRIVATE = private
,@.PLAYERLIMIT = CASE WHEN p.player_limit = 0 OR p.player_limit IS
NULL THEN 'No Limit' ELSE CAST(p.player_limit AS NVARCHAR(33)) END
,@.PLAYERCOUNT = ISNULL(pc.player_count,'')
from
tPools p
LEFT OUTER JOIN
(
SELECT
POOLID,
COUNT(*) player_count
FROM
tProfilePool
GROUP BY
POOLID
) pc
ON
p.pool_id = pc.poolid
where
pool_id = 2
SELECT
@.PRIVATE, @.PLAYERLIMIT, @.PLAYERCOUNT
========================================
Execution Result:
Y No Limit 1You need to specify OUTPUT in the EXEC
EXEC DBO.USP_GET_POOL_INFO 2, @.PRIVATE OUTPUT , @.PLAYERLIMIT OUTPUT ,
@.PLAYERCOUNT OUTPUT