Error in Insert query in a table by executing one system proc in sybase - sap-ase

I am getting error while inserting data inside table TableSizes with below query,
Could anyone please help me in this regard.
declare #name varchar(256)
select #name ='TestTable'
begin
insert into workdb..TableSizes
exec sp_spaceused #name
end
go
Msg 156, Level 15, State 2
Server '<ServerName>', Line 5
Incorrect syntax near the keyword 'exec'.
Thanks in advance.

So what you are trying to do here is to insert a proc result set into a table, which is not allowed unless the table is a proxy table. If not, then complete the insert statement.

Related

How do I add extra rows to an Excel sheet via ODBC?

I used ODBC to create a sheet in Excel and add a row to it.
Literally the commands were just:
create table 'update5' ('age' NUMBER);
insert into 'update5'.'age' values (1);
This works and I can see the rows in the sheet and via DBVisualiser and my ODBC query results.
Later, I wrote more SQL to add another row like so:
insert into 'update5' ('age') values (2);
but I get the error:
[Microsoft][ODBC Excel Driver] Cannot expand named range.
I do not know why named ranges are being used, is there a way I can set ODBC to not use them?
Without knowing more about what your doing, what you're working with, and your end-goal I can't give a definite answer - however, if you're saying this works fine as-is:
create table 'update5' ('age' NUMBER);
insert into 'update5'.'age' values (1);
...then it stands to reason that this:
insert into update5 values (2);
...will not work because your missing:
quotation marks (which may or may not be optional in your environment), and,
the field name to which you want to export.
In the first insert into statement you have:
'update5' <-- the destination table
.'age' <-- the destination field
values (1); <-- the value to insert
...so if you're just trying to add a record with the number 2 to the same field, use the same code:
insert into 'update5'.'age' values (2);
More Information:
w3schools : SQL INSERT INTO Statement
TutorialsPoint : SQL INSERT Query (Tutorial)
Found it.
When you create an table in Excel via ODBC you create a named range of the same name within that table.
When you try to insert like this:
insert into 'update5'.'age' values (2);
It is interpreted as you trying to add to the the named range called update5 which is inside the table update5.
You need to use the syntax:
insert into [update5$].'age' values (2);
to add values to the table called update5.

How can I insert multiple records with a single INSERT statement in Sybase ASE

This feature is available in MySQL as shown in this post and according to the Sybase documentation it should also be supported, however Sybase don't provide a worked example so you have to interpret the following:
Syntax 1 Insert a single row, or multiple rows, with the specified expression column values. Multiple rows, if specified, are delimited by additional parentheses
So I interpret "additional parentheses" as expecting the following code to work
create table #tmp_codes (
code varchar(12) NULL
)
insert into #tmp_codes (code)
values
('AAA'),
('BBB'),
('CCC')
However it errors with
Incorrect syntax near ',' on line 7
I'm using Sybase ASE 15 and cannot see any reference to inserting multiple rows on this support page for the INSERT statement
Is this feature available in Sybase?
Your first Sybase doc link is ASA not ASE documentation.
In ASE you can insert multiple rows only with insert - select statement.
This seems long ago asked question but may be useful for reference.
Create a Text file with multiple Rows.
Run this command from Sybase ASE:
INPUT INTO TableName
FROM FilePath\FileName FORMAT TEXT
Example:
INPUT INTO TempTable FROM c:\test.txt FORMAT TEXT

Sybase Dropping Temporary Table

Does anybody face an issue when you drop a temporary table at the Sybase ASE 12 it still persists at a current session. So you encounter "Table already exists" when trying to select data into it again
Well, you need to read the manuals, at least the syntax for the commands you expect to use, before you write code. Otherwise you will face issues at every turn. It depends on what you are trying to do.
SELECT ... INTO #MyTable creates a table and succeeds because it does not exist. So a second SELECT ... INTO #MyTable will try to create #MyTable, find that it exists, and fail.
If you want to perform a second SELECT into the same table, TRUNCATE the table, then use SELECT ... INTO EXISTING TABLE #MyTable.
Or DROP TABLE and skip the EXISTING TABLE modifier.
If you want the table to contain the sum of several SELECTS, obviously, skip the TRUNCATE.
I normally do this:
1) CREATE TABLE #temptable (
....
)
INSERT INTO #temptable
SELECT .....
This will never give error.
This solves another possible error . If the WHERE clause accompanying the "select INTO " yields no rows, the temporary table will not have zero rows but the temporary table won't be created at all. This could make the stored proc blow up later.

Forgot to Drop Temp Table, now I can't get Rid of It

I wrote a stored procedure and created a temp table #TempHitRatioTable. I forgot to put the drop table statement in my procedure and now no matter what I do I can't get rid of the temporary table and can't re-execute the procedure. I have tried completely disconnecting from the server and reconnecting, but it is still there. I have tried the following statements:
IF OBJECT_ID('tempdb..#TempHitRatioTable') IS NOT NULL
DROP TABLE #TempHitRatioTable
and I also tried:
IF EXISTS(SELECT * FROM sys.tables WHERE name LIKE '#TempHitRatioTable%')
DROP TABLE #TempHitRatioTable
IF EXISTS(SELECT 1 FROM sys.objects WHERE OBJECT_ID = OBJECT_ID(N'#TempHitRatioTable') AND type = (N'U')) DROP TABLE #TempHitRatioTable
But I still can't get rid of that table. My stored procedure is no longer running, nor is the crystal report I tried to run it on, but nothing seems to work. Please help.
I figured out what was happening. I had put the create table at the bottom of the cursor inside of it, so each time the cursor iterated throught to add a new record the table was already created. I moved the create table to the top of the stored procedure, before I created the cursor, added the statement: IF OBJECT_ID('tempdb..#TempHitRatioTable') IS NOT NULL DROP TABLE #TempHitRatioTable at the top of the procedure, then I added the drop table statement to the end of the procedure and all is well. I can run it as many times as I want and get all my results back not just one record. Silly me :-)

Adding columns to a sybase table with unique auto_identity index option

I've inherited a Sybase database that has the 'unique auto_identity index' option enabled on it. As part of an upgrade process I need to add a few extra columns to the tables in this database i.e.
alter table mytable add <newcol> float default -1 not null
When I try to do this I get the follow error:
Column names in each table must be unique, column name SYB_IDENTITY_COL in table #syb__altab....... is specifed more than once
Is it possible to add columns to a table with this property enabled?
Update 1:
I created the following test that replicates the problem:
use master
sp_dboption 'esmdb', 'unique auto_identity indexoption',true
use esmdb
create table test_unique_ids (test_col char)
alter table test_unique_ids add new_col float default -1 not null
The alter table command here produces the error. (Have tried this on ASE 15/Solaris and 15.5/Windows)
Update 2:
This is a bug in the Sybase dbisql interface, which the client tools Sybase Central and Interactive SQL use to access the database and it only appears to affect tables with the 'unique auto_identity index' option enabled.
To work around the problem use a different SQL client (via JDBC for example) to connect to the database or use isql on the command line.
Should be no problem to ALTER TABLE with such columns; the err msg indicates the problem regards something else. I need to see the CREATE TABLE DDL.
Even if we can't ALTER TABLE, which we will try first, there are several work-arounds.
Responses
Hah! Internal Sybase error. Open a TechSupport case.
Workaround:
Make sure you get jthe the exact DDL. sp_help . Note the IDENTITY columns and indices.
Create a staging table, exactly the same. Use the DDL from (1). Exclude the Indices.
INSERT new_table SELECT old_table. If the table is large, break it into batches of 1000 rows per batch.
Now create the Indices.
If the table is very large, AND time is an issue, then use bcp. You need to research that first, I am happy to answer questions afterwards.
When I ran your sample code I first get the error:
The 'select into' database option is not enabled for database 'mydb'. ALTER TABLE with data copy cannot be done. Set the 'select into' database option and re-run
This is no doubt because the data within your table needs copying out because the new column is not null. This will use tempdb I think, and the error message you've posted refers to a temp table. Is it possible that this dboption has been accidentally enabled for the tempdb?
It's a bit of a shot in the dark, as I only have 12.5 to test on here, and it works for me. Or it could be a bug.

Resources