Please be aware that the content in SAP SQL Anywhere Forum will be migrated to the SAP Community in June and this forum will be retired.

Update: The comments and suggestions in this Q&A conversation led directly to a blog post about OPENSTRING and CROSS APPLY.

This was originally a "How do I ... ?" question until John Smirnios provided a gentle "You can't" answer, so now it is a "Product Suggestion".

Here is the text of the original question...

Here is a working FOR loop that invokes OPENSTRING to repeatedly analyze a tab-delimited string stored in the column raw_text.line_text, in

Is there a way to eliminate the FOR loop and use a single set-oriented INSERT statement that invokes OPENSTRING ( VALUE raw_text.line_text ) instead of using a local string variable OPENSTRING ( VALUE @line_text )?

If LATERAL is the answer, the syntax escapes me :)

CREATE TABLE raw_text (
   line_number BIGINT NOT NULL 
                  PRIMARY KEY CLUSTERED,
   line_text   LONG VARCHAR NOT NULL DEFAULT '' );

/* sample input...
128,Play - not available now        WAAV    Mon-Fri 10am    2 hours Leland  NC  980 AM      

CREATE TABLE radio_programs (
   station     VARCHAR ( 100 ) NULL,
   dates       VARCHAR ( 100 ) NULL,
   times       VARCHAR ( 100 ) NULL,
   duration    VARCHAR ( 100 ) NULL,
   city        VARCHAR ( 100 ) NULL,
   state       VARCHAR ( 100 ) NULL,
   frequency   VARCHAR ( 100 ) NULL );

SELECT raw_text.line_number AS @line_number,
       raw_text.line_text   AS @line_text   
  FROM raw_text
 ORDER BY raw_text.line_number

INSERT radio_programs
   SELECT @line_number,
     FROM OPENSTRING ( VALUE @line_text )
             WITH ( TABLE radio_programs (
                       filler() ) )
             OPTION ( DELIMITED BY '    ' ) AS programs;


/* sample output...
128,'WAAV','Mon-Fri','10am','2 hours','Leland','NC','980 AM'

asked 14 Nov '10, 13:58

Breck%20Carter's gravatar image

Breck Carter
accept rate: 20%

edited 22 Nov '10, 16:40

Oh, I never said "you can't" -- I only gave as close an answer as I could come up with. I'm no SQL guru by far.

(17 Nov '10, 14:58) John Smirnios

@John: SQLA 2 suggestion: A badge "Consider me a SQL guru" that anyone can apply to his profile - possible with further flavours "Code guru", "Performance guru" and so on...

(17 Nov '10, 15:10) Volker Barth

@John: But honestly, personally, I would expect each and every experienced iAnywhere staff member to be a SQL guru. It may not be reasonable, but, man, that's the admiration me (and others, I guess) are willing to show for your great development team:)

(17 Nov '10, 15:13) Volker Barth

@Volker: I work on the low level nuts and bolts of the server: tasking models, caching, IO, recovery, internationalization, indexing, validation, backup, that sort of stuff. You can think of it as the "operating system" that the SQL query execution engine runs on plus some other specific tools. The SQL language itself is far removed from that and I've never really need to know the finer points of SQL. I'll even admit that I had no idea what a lateral derived table was until Glenn and Ivan explained it to me today in the context of Breck's question. :)

(17 Nov '10, 18:37) John Smirnios

@John: Well, I guess I have a rough idea what different developers in the iAnywhere teams are dealing with, and the answers you have been posted in both the newsgroups and here (as far as I can recall) naturally fit to the work-field description you've pointed out above (Something to add to your profile?). And I'm quite aware that this low level stuff is a totally different beast than SQL query execution. - That being said, I still think us customers like to treat all of you as our SQL heroes... But I see, "SQL Anywhere heroes" would more correct:)

(18 Nov '10, 08:22) Volker Barth

@Volker: The badge should be called "We are not worrrrthy!" and should be awarded immediately to all iAnywhere folks, immediately upon first login. Or, to avoid misinterpretation, to everyone ELSE instead :)

(18 Nov '10, 09:38) Breck Carter

@John: Your answer was a heck of a lot better than "invalid parameter" which was what I was getting. Bounty-worthy, in fact, except it was time to embarrass Ivan :)

(18 Nov '10, 09:45) Breck Carter
More comments hidden
showing 5 of 7 show all flat view

You should be able to combine the statements using something like the following:

    declare local temporary table T_strs( str long varchar );
    insert into T_strs values ('1,a1\n2,a2'), ('3,b1\n4,b2');

select  *
    from    T_strs T cross apply openstring( value T.str ) 
                                 with( a char(10) ) as DT

Unfortunately, there is a bug^H^H^H unintended limitation that prevents the above from working correctly: instead, the server returns an error "invalid parameter".

There is a work-around for the limitation, you can use T.str || '' in the value clause of OPENSTRING. The requirement is that the value expression must not be a simple expression, nor can it be used elsewhere in the query. With this workaround, you can use something based on the following (here I show the LATERAL syntax in case you prefer it). I tend to prefer CROSS APPLY myself but LATERAL is from the ANSI standard.

select  *
from    T_strs T, lateral( openstring( value T.str||'' ) 
                           with( a char(10) ) as DT1 ) DT2

Look forward to this limitation being corrected in a future version of the server.

permanent link

answered 17 Nov '10, 16:58

Ivan%20T.%20Bowman's gravatar image

Ivan T. Bowman
accept rate: 39%

@Ivan: Does "being corrected in a future version of the server" refer to an EBF or a coming maintenance release or even main version?

(18 Nov '10, 08:12) Volker Barth

Ha ha ha ha ha ha [end maniacal laughing sound]... I always assume messages like "invalid parameter" are computer for "you are out of your depth"... occasionally, apparently, that assumption is incorrect :)

(18 Nov '10, 09:32) Breck Carter

@Volker: My guess is "EBF" but since there is a perfectly easy workaround, maybe they won't hold up the next EBF for this... or, heaven forfend, hold up the release of 12.0.1... I say "perfectly easy" before trying it, but after all, it's Ivan speaking :)

(18 Nov '10, 09:36) Breck Carter
Comment Text Removed
Comment Text Removed
Comment Text Removed
Comment Text Removed

In line with Volker's polite request, I am tardily adding some bug fix information. The issue that Breck reported is fixed by CR648799. The fix appears in 12.0.1 GA, 12.0.0(3268), and 11.0.1(2541).

(02 Apr '11, 14:38) Ivan T. Bowman
Replies hidden

Thanks a lot:)

(02 Apr '11, 14:57) Volker Barth

I think I can give you an almost but not quite approach that may be of use to you. There is a form of LOAD TABLE that is, strictly speaking, undocumented though you will see it if you translate a log that contains a LOAD TABLE ... WITH CONTENT LOGGING. It looks like the following:

LOAD TABLE table_name USING COLUMN string_column FROM input_table ORDER BY column_list

column_list must be a verifiably unique set of values (ie, there is a primary key or unique index on non-nullable columns included within column_list) and string_column must be a string type (char, nchar, or binary).

Effectively, the values of string_column are concatenated to produce the data stream that will be processed by LOAD TABLE. In your case, your input doesn't appear to have row delimiters at the end of raw_text.line_text; however, if it did you could possibly get away with the syntax above. If there is an ENCODING clause and string_column is CHAR or NCHAR, the encoding must match the encoding of string_column. If string_column is BINARY, any encoding is permitted.

As I said, you won't find it documented in the LOAD TABLE docs; however, since our tools generate that syntax it's not exactly a secret. I'll add a standard disclaimer that undocumented behaviour is subject to change without notice though.


permanent link

answered 17 Nov '10, 04:34

John%20Smirnios's gravatar image

John Smirnios
accept rate: 37%


Thanks! ...I can always use another blog topic, and there's NOTHING like "Undocumented" in a title to get people's attention... well, almost nothing :)

(17 Nov '10, 12:00) Breck Carter

GRANT CONNECT TO ... IDENTIFIED BY ENCRYPTED ... is also undocumented but you have probably seen it countless times in reload scripts. The form of LOAD TABLE described above was created to solve a very specific problem and I don't think it was expected to be of general use to users.

(17 Nov '10, 21:36) John Smirnios
Your answer
toggle preview

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here



Answers and Comments

Markdown Basics

  • *italic* or _italic_
  • **bold** or __bold__
  • link:[text]( "title")
  • image?![alt text](/path/img.jpg "title")
  • numbered list: 1. Foo 2. Bar
  • to add a line break simply add two spaces to where you would like the new line to be.
  • basic HTML tags are also supported

Question tags:


question asked: 14 Nov '10, 13:58

question was seen: 3,628 times

last updated: 02 Apr '11, 14:57