-1

Is there a coding naming convention principal for SQL variables? Can variables be named the same as column names, I heard in different companies, this may cause confusion. However in C# below, its a generally accepted principle, resource below.

create table dbo.Product
( 
    ProductId int primary key null,
    ProductName varchar(25) not null,
    ProductDescription varchar(255) not null
)


create procedure dbo.ProductNameUpdate 
@ProductId int, @ProductName varchar(255)
as

update dbo.Product
set 
ProductName = @ProductName
where ProductId = @ProductId 

https://msdn.microsoft.com/en-us/library/ms229060(v=vs.100).aspx https://www.dotnetperls.com/constructor

public Product(int ProductId, int ProductName)
        {
            this.ProductId= ProductId;
            this.ProductName= ProductName;
        }
  • Well, clearly variable names can be named the same as column names, as your example attests. There's no confusion (other than your odd choice of brace spacing), because the variables are always disambiguated, either by an @ sign or the this keyword. – Robert Harvey Jul 12 '18 at 0:28
  • I’ve never heard the term “brace spacing” @RobertHarvey and google failed me. What’s it referring to? – Paul Aug 12 '18 at 15:34
  • @Paul Indentation. – Robert Harvey Aug 12 '18 at 15:35
  • Oh, I assumed that was just a transcription error due to Stack’s code formatter. – Paul Aug 12 '18 at 15:36
1

Not only is it acceptable to have column names the same as variables, most ORM tools will do so by default, following the naming conventions of whatever language is building the application.

0

Different languages have different naming conventions. For example, in Python fields should be formatted using snake case like product_id and product_name. If you ignore such conventions your code is going to be less idiomatic, and unless you use camel case for fields everywhere it is going to be inconsistent. Both of these will hurt readability. Of course, if you happen to only ever use a single language and a single database which happen to have the same naming conventions, go for it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.