Re: Dynamic column specification in table update

From: Tibor Karaszi (tibor_please.no.email_karaszi_at_hotmail.nomail.com)
Date: 10/06/04


Date: Wed, 6 Oct 2004 18:49:11 +0200

Yes, you need braces to EXEC a string. Again print the string to you know what you are trying to
execute. A varchar can be up to 8000 if declare properly. If you need more, you can use below
technique:

EXEC (@str1 + @str2)

-- 
Tibor Karaszi, SQL Server MVP
http://www.karaszi.com/sqlserver/default.asp
http://www.solidqualitylearning.com/
"Alec MacLean" <alec.maclean@NO-SPAM-copeohs.com> wrote in message 
news:%23r%23vTO8qEHA.2580@TK2MSFTNGP15.phx.gbl...
> Changing the EXEC to include braces round the sql command variable helps (partially):
>
> SELECT @DSQL = 'UPDATE #tRes SET [' + CAST(@H AS NVARCHAR(30)) + ']'
>  SELECT @DSQL = @DSQL +
> <snip blah>
> ...
>
> EXEC ( @DSQL )
>
> But then I get Error 170 (incorrect syntax).  The output string is the first 256 chars of the DQL 
> string, which happens to be on the 'W' of the where clause...
> Is the 256 truncation a factor of the error message processing (so a non-factor), or a limit of 
> QA?
>
> Thanks
>
> Al
>
> "Alec MacLean" <alec.maclean@NO-SPAM-copeohs.com> wrote in message 
> news:eXcVzG8qEHA.3244@tk2msftngp13.phx.gbl...
>> Thanks Mal...
>>
>> I've not used Dynamic SQL before, so been reading up a bit.
>> I've tried your suggestion, but I get the server message 203 ("name is not a valid identifier")
>>
> <snip>
>>
>> Thanks for your help.
>>
>> Al
>>
>>
>
> 


Relevant Pages

  • Re: BETWEEN in dynamic SQL
    ... I have a mess in printed SQL. ... > Instead of EXEC sp_executesql @sql ... >> Syntax error converting datetime from character string. ...
    (microsoft.public.sqlserver.programming)
  • Re: Oracle NULL vs revisited
    ... (p_msg VARCHAR2) ... SQL> exec distinguish_emptiness; ... String with length 50 ...
    (comp.databases.oracle.server)
  • Re: Dynamic SQL & Linked Servers
    ... I tried to create linked server as well as the view on the table on ... exec -- the t-sql string to create view etc. ... Microsoft SQL Server Support Professional ...
    (microsoft.public.sqlserver.programming)
  • Re: Name von Tmp-Tabelle aus Select ... Into
    ... in einen String packen und diesen mit EXEC ausfuehren? ... Bei Tabellennamen geht dies so nicht? ... Die Gefahr bei dynamischem SQL ist nämlich SQL Injection. ...
    (microsoft.public.de.sqlserver)
  • Re: Problems with Delete Command
    ... The SQL could get fairly messy if you need to construct it in code, ... ContactID, and WebComID, and create your on-the-fly SQL on that saved query, ... to find the list of ContactIDs from the junction table, ... This is a style/readability thing: if you are going to use string ...
    (microsoft.public.access.tablesdbdesign)