Click here to Skip to main content
15,892,575 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,

I am working in an oracle to sql server migration task, and I am stuck at a place where i need to convert a type in oracle which looks like:

create or replace type Q_ARRAY
is varray(2000) of VARCHAR2(11);

in Oracle.

Any help on this is appreciated..
thanks,
Dave

What I have tried:

I didnt get an exact conversion of varray. I am new to sql. sorry
Posted
Updated 13-Jan-17 1:13am
Comments
Richard Deeming 11-Jan-17 12:15pm    
I don't think there's a direct equivalent. Maybe a table variable[^] would work?

Table Variables In T-SQL[^]

As far as I know an exact match for VARRAY does not exist in SQL Server, at least not with all the collection functions.

So the decision what to use depends highly on what kind of operations you're going to make with the array. In most scenarios I believe that using a table variable would be sufficient. Have a look at the following example
SQL
-- Define the type, valid only inside the batch
DECLARE @SomeList TABLE ( col1 int );

-- Add some data
INSERT INTO @SomeList (col1) values
(1), (2), (3), (4)

--Set based operation
BEGIN
   SELECT * FROM @SomeList
END;

-- The result is 
--   col1
--   1
--   2
--   3
--   4

--Cursor based operation to handle individual rows
BEGIN
   DECLARE @col1value int;
   DECLARE ListCursor CURSOR FOR SELECT col1 FROM @SomeList;
   OPEN ListCursor;
   FETCH NEXT FROM ListCursor INTO @col1value;
   WHILE @@FETCH_STATUS = 0  BEGIN 
      PRINT 'Col1 value is '+ CAST(@col1value AS varchar(100))
      FETCH NEXT FROM ListCursor INTO @col1value;
   END;
END ;
-- the result is 
--    Col1 value is 1
--    Col1 value is 2
--    Col1 value is 3
--    Col1 value is 4
GO

The type can also be persistent just like in Oracle. In such case you need create the type separately. Have a look for an example at How to pass multiple records to a Stored Procedure[^]

Also remember that SQL Server does not have the concept of packages so you cannot define public types and the code in the same module.
 
Share this answer
 
Comments
daviseban 12-Jan-17 6:16am    
Thanks Mike Wendelius for your solution.
The VARRAY is used to pass an array of parameter values from java to stored proc.
Please take a look at this java code here:

String[] array = list.toArray(new String[list.size()]);
ArrayDescriptor arraydesc = ArrayDescriptor.createDescriptor
("MY_DBSCHEMA.Q_ARRAY", connection);
oracle.sql.ARRAY argArray = new oracle.sql.ARRAY(arraydesc,connection, array);

I am using the ojdbc6.jar here.
But this is highly oracle specific.
Can we achieve the same by using table TYPES in sql?

Basically, i need to pass an array of String..
thanks in advance
Wendelius 12-Jan-17 8:09am    
I have to admit that my Java skills are in rust so I'm not entirely sure about the current situation. However it seems that table valued parameters are supported starting from JDBC 6.0 (MS). See Using Table-Valued Parameters[^]
daviseban 12-Jan-17 9:22am    
ok.. thank you so much.
Wendelius 12-Jan-17 9:35am    
You're most welcome :)
I tried a lot of things and got a solution at last..

Instead of VARRAY in Oracle a I created TABLE TYPE in SQL server like this:

CREATE TYPE dbo.Q_ARRAY
AS TABLE
(
  Q_ID VARCHAR(11)
);
GO


This is used in a stored proc as an argument and I need to pass the string array to this stored proc from java.
I was able to do this with the below java code:

SQLServerDataTable qDT = new SQLServerDataTable(); 
				vehDT.addColumnMetadata("Q_ID", java.sql.Types.VARCHAR);
				vehDT.addRow("11122369");
				vehDT.addRow("45523264");
				
				
				SQLServerPreparedStatement spStat = (SQLServerPreparedStatement)con.prepareStatement("{call [dbo].[process_qRequest](?)}");
				spStat.setStructured(1, "dbo.Q_ARRAY", qDT);


This is working like a champ!!
Here I am using sqljdbc41 jar.
Please note that the old sql jdbc jars doesnt provide many of these functionalities.

thanks,
Dave
 
Share this answer
 
can any one help with the download link for sqljdbc4.jar?
 
Share this answer
 
Comments
CHill60 16-Nov-17 4:12am    
If you have a question of your own then use the red "Ask a Question" link at the top of the page. Be sure to read the guidelines for posting questions which will be shown when you click the link...Especially the one about doing your own research.
Do not post questions or comments as "Solutions" to other posts

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900