Concatenate contents of column if it exists - 'invalid column name' error

  • But from my limited testing, I think that having multiple stored procedures (one for each possible scenario and one "master" one that is used to call one of the others) is probably going to be the easiest way.

    the master stored procedure would look at sys.columns OR COLUMNPROPERTY() and depending on which columns exist determines which sub-stored procedure you call.  I did a quick test on my machine of this and it seems to work.

    Doing it in a  coded language (such as VB.NET) shouldn't be too hard either.  If your input files have consistent columns with the exception of the "footer" column, you split the string on your delimiter and have IF or CASE statements depending on how many elements are in the array and concat the last few into 1 if appropriate.  Then push that out to SQL or to a text file to import into SQL.

    The above is all just my opinion on what you should do. 
    As with all advice you find on a random internet forum - you shouldn't blindly follow it.  Always test on a test server to see if there is negative side effects before making changes to live!
    I recommend you NEVER run "random code" you found online on any system you care about UNLESS you understand and can verify the code OR you don't care if the code trashes your system.

Viewing post 16 (of 15 total)

You must be logged in to reply to this topic. Login to reply