What is equivalent to 'as table of' in SQL Server - sql

I have a following query in Oracle that I need to convert to SQL Server
Oracle:
create or replace
TYPE "PARAMS" as table of varchar2(3000)
I am unsure how to convert 'as table of' object to SQL Server.

I'm pretty sure that's Oracle's way of creating user defined table types.
In T-SQL it would look like this:
CREATE TYPE Params AS TABLE
(
[value] Nvarchar(3000)
)

Related

Failed to execute query. Error: String or binary data would be truncated in table xdbo.user_info', column 'uid'

I have problem inserting values in my SQL server database on Azure, I am getting the following error:
Failed to execute query. Error: String or binary data would be truncated in table 'dummy_app.dbo.user_info', column 'uid'. Truncated value: 'u'.
The statement has been terminated.
I don't understand where I am wrong, I just created the server, and I am trying to experiment but cant fix this.
if not exists (select * from sysobjects where name='user_info' and xtype='U')
create table user_info (
uid varchar unique,
name varchar,
email varchar
)
go;
INSERT INTO dbo.user_info(uid, name, email) VALUES('uids', 'name', 'email') go;
Creating the table works fine, the only thing that doesn't work is the second command INSERT
I suspect that the reason is that you haven't defined a lenght for varchar and it defaults to 1 as length. Therefore your value gets truncated.
Set a varchar length to something like varchar(200) and you should be good to go.
This looks like the fact that the CREATE portion of your procedure for the table doesn't include a length of varchar, so you'd have to specify a length such as varchar(50) since the default is 1. Refer to the official MS docs in the link, in the remarks.
docs.miscrosoft.com
Also, here is the syntax for the CREATE TABLE in Azure which might be helpful as well.
Syntax of Azure CREATE TABLE

How to declare variable in vectorWise like SQL Server?

I am new to VectorWise database.
I need to declare variable and pass value in it same like we do in a SQL Server database. Please help me how to do this in VectoreWise database. I am using Squirrel as SQL client.
I need to do it like we do in SQL Server:
Declare #name varchar (100)
set #name ='ABC'
select #name
Output: ABC
I am not particularly familiar with Actian Vector, so I'm not sure if it has a scripting language. I don't see declare as a supported statement.
If you only need a parameter in a single select, you can use a CTE. For your example:
with params as (
select 'ABC' as name
)
select params.name
from params;
I'm not sure if this helps you, but it might.

Result Set of procedure into XML

how can we convert the result set of a procedure into an XML without creating a temporary table since the result set may vary in their structure based on the inputs ? the scenario must work in both on premise and azure hosted SQL server databases.
CREATE PROCEDURE SPSample(#Scope VARCHAR(2))
AS
BEGIN
IF(#Scope='A')
SELECT 1 AS ID,2 AS NAME
IF(#Scope='B')
SELECT 1 AS CustomerID,'Sample#sample.com' AS Email,'121314' AS ContactNumber
END
I believe you are asking to use FOR XML or FOR XML AUTO:
https://learn.microsoft.com/en-us/sql/relational-databases/xml/for-xml-sql-server?view=sql-server-2017
create table mytable(col1 int, col2 nvarchar(20), col3 money)
insert into mytable(col1, col2, col3) values (1, 'string', 123.45)
select * from mytable
for xml auto
Emits:
You can do this on any query result set:
select * from sys.objects
for xml auto
This will emit that structure similarly.
i need a way that the result set from a stored procedure directly converted to XML object without creating a table structure since the result set from procedure may vary
There's no way in TSQL to do that. TSQL can only convert to XML using a SELECT, not an EXECUTE. You could do this with a loopback linked server or CLR, but those are hacks, and not available in Azure SQL DB. You'll have to find another way. EG
CREATE PROCEDURE SPSample(#Scope VARCHAR(2), #ResultsAsXml bit = 0)
. . .

Convert HEX value to CHAR on DB2

In connection with data replication from SQL Server to DB2 I have the following question:
On DB2 I have a table containing (for simplicity) two columns: COL1 and COL2.
COL1 is defined as CHAR(20). COL2 is defined as CHAR(10).
COL1 is replicated from SQL by converting a string into hex, e.g. "abcdefghij" to "6162636465666768696A" or "1111111111" to "31313131313131313131" by using the following SQL query:
CONVERT(char(20), cast(#InputString as binary) 2)
where #InputString would be "abedefghij".
In other words COL1 contains the hex value, but as a string (sorry if the wording is incorrect).
I need to convert the hex value back to a string and put this value into COL2.
What should the SQL query be on DB2 to do the convertion? I know how to do this on SQL Server, but not on DB2.
Note: The reason the hex-value is not pre-fixed with "0x" is because style 2 is used in the CONVERT statement.
select hex('A') from sysibm.sysdummy1;
returns 41.
and
select x'41' from sysibm.sysdummy1;
gives you 'A'. So you can put that in a for loop and loop through each pair of hex characters to arrive at your original string. Or you can write your own unhex function.
Taken from dbforums.com /db2/1627076-display-hex-columns.html (edit Nov 2020: original source link is now a spam site)
DB2 has built-in encoding/decoding.
For OPs question, use....
select CAST(ColumnName as char(20) CCSID 37) as ColumnName from TableName where SomeConditionExists
http://www-01.ibm.com/support/knowledgecenter/SSEPEK_10.0.0/com.ibm.db2z10.doc.intro/src/tpc/db2z_introcodepage.dita
This is one of most close topics to subject of my problem:
I have lost 2 days to figure out how to migrate XML files stored in DB2 BLOB field using SQL Developer.
(Yes, migrating to and doing the queries from SQL Developer - we are migrating data to Oracle from DB2, so we were using this tool)!
How to show XML file/string stored in BLOB?
Let's start with, what the problem was:
Data in BLOB was a XML file.
When selected in query, got:
When casted, like:
select CAST(BLOBCOLUMN as VARCHAR(1000)) from TABLE where id = 100;
output was in HEX:
Nothing worked... Not even the solution from the links in this topic.
!NOTHING!
By mistake found a solution:
CREATE FUNCTION in DB2:
CREATE FUNCTION unhex(in VARCHAR(32000) FOR BIT DATA)
RETURNS VARCHAR(32000)
LANGUAGE SQL
CONTAINS SQL
DETERMINISTIC NO EXTERNAL ACTION
BEGIN ATOMIC
RETURN in;
END
Run SELECT:
select UNHEX( CAST(BLOBCOLUMN as VARCHAR(32000) FOR BIT DATA)) from TABLE where id = 100;
Result:
I use this to convert FOR BIT DATA to characters:
cast (colvalue as varchar(2000) ccsid ebcdic for sbcs data)

TSQL - Case on Ntext (SQL 2005)

Stored Procedures in SQL 2005 - with field type NText
Im Writing a stored procedure to tidy up some data before importing it into Microsoft CRM.
So far all works fine.
However i need to do a case statement on a nText Field. It needs to check this field against about 3 or 4 text values and set a new field (already in the destination table) which is also an nText field.
However i am getting the error
"The data types ntext and varchar are incompatible in the equal to operator.
I have come across a few articles however their solutions all seem very complex.
Thanks for your help and advice in advanced.
I recommend, if at all possible, replacing the NTEXT type with NVARCHAR(MAX), since NTEXT is not a first class type and NVARCHAR is. This should be easy to do with an ALTER TABLE statement.
Most higher level code shouldn't care about the type change. Any procedural code that uses READTEXT, WRITETEXT, etc. to deal with the NTEXT columns can be simplified to just basic selects and updates.
If the type change is not possible you may have to wrap the comparisons and assignments with CAST() or CONVERT() operators, which is ugly.
NTEXT is deprecated in SQL Server 2005. You should use NVARCHAR(MAX) instead (NVARCHAR(MAX) can be used in CASE). Is it possible for you to change the type?
this works as well
CREATE TABLE #TEMP
(
MyDummy NTEXT
)
INSERT INTO #TEMP (MyDummy) Values ('test')
SELECT
CASE CAST(MyDummy AS NVARCHAR(MAX)) WHEN 'test' THEN 'ok' ELSE 'NOK' END MyTest
FROM #temp
drop table #temp