Thursday, March 8, 2012

Best Practice Analyzer rule doesnt make sense

I'm running the Best Practice Analyzer on a sql 2k server in an attempt to
prepare it for 2005, and one of many rules that dont make sence is:
Object contains an INSERT statement without explicit specification of target
column list.
This rule checks objects for use of INSERT statements without explicit
specification of target column list.
When inserting into a table or view, it is recommended that the target
column_list be explicitly specified. This results in more maintainable code.
Exactly how do you explicitly specify a column? Here's the code at fault:
CREATE TABLE #Tmp ( ID int, [Name] varchar(3))
INSERT INTO #Tmp (#Tmp.ID, #Tmp.Name) VALUES (1, 'No')
INSERT INTO #Tmp (#Tmp.ID, #Tmp.Name) VALUES (2, 'Yes')
SELECT * FROM #Tmp
Can anyone explain why it doesnt like this code?
moondaddy@.nospam.nospam
Try this:
INSERT INTO #Tmp ([ID], [Name]) VALUES (1, 'No')
Both ID and Name are reserved words and should be avoided where possible.
Andrew J. Kelly SQL MVP
"moondaddy" <moondaddy@.nospam.nospam> wrote in message
news:OzHTw2K7FHA.2384@.TK2MSFTNGP12.phx.gbl...
> I'm running the Best Practice Analyzer on a sql 2k server in an attempt to
> prepare it for 2005, and one of many rules that dont make sence is:
> Object contains an INSERT statement without explicit specification of
> target column list.
> This rule checks objects for use of INSERT statements without explicit
> specification of target column list.
> When inserting into a table or view, it is recommended that the target
> column_list be explicitly specified. This results in more maintainable
> code.
> Exactly how do you explicitly specify a column? Here's the code at fault:
> CREATE TABLE #Tmp ( ID int, [Name] varchar(3))
> INSERT INTO #Tmp (#Tmp.ID, #Tmp.Name) VALUES (1, 'No')
> INSERT INTO #Tmp (#Tmp.ID, #Tmp.Name) VALUES (2, 'Yes')
> SELECT * FROM #Tmp
> Can anyone explain why it doesnt like this code?
> --
> moondaddy@.nospam.nospam
>
|||Thanks. I think that did it.
btw: Good to see microsoft.public.sqlserver.integrationsvcs up and running!
Looks like I will spend some time there.
moondaddy@.nospam.nospam
"Andrew J. Kelly" <sqlmvpnooospam@.shadhawk.com> wrote in message
news:%23TmGwTL7FHA.2816@.tk2msftngp13.phx.gbl...
> Try this:
> INSERT INTO #Tmp ([ID], [Name]) VALUES (1, 'No')
>
> Both ID and Name are reserved words and should be avoided where possible.
> --
> Andrew J. Kelly SQL MVP
>
> "moondaddy" <moondaddy@.nospam.nospam> wrote in message
> news:OzHTw2K7FHA.2384@.TK2MSFTNGP12.phx.gbl...
>

No comments:

Post a Comment