Please help, how to correct this procedure??
Right now, I'm getting an error:
Implicit conversion from data type varchar to varbinary(max) is not allowed. Use the CONVERT function to run this query.
Code:
CREATE PROCEDURE SP_FILEUPLOAD
#UID int,
#APPCODE varchar(50) = NULL ,
#APPEXPIRED datetime = NULL ,
#SIGNIMAGE image = NULL ,
#SIGNFILE varbinary(MAX) = NULL ,
#HEADUID int,
#PRINCIPALFROM money,
#PRINCIPALTO money,
#EXCEPTIONUSER bit,
#LastUpdate datetime
AS
BEGIN
SET NOCOUNT ON
INSERT INTO APPUSERDTL
(UID,
APPCODE,
APPEXPIRED,
SIGNIMAGE,
SIGNFILE,
HEADUID,
PRINCIPALFROM,
PRINCIPALTO,
EXCEPTIONUSER,
LastUpdate)
VALUES('#UID',
'#APPCODE',
'#APPEXPIRED',
'#SIGNIMAGE',
'#SIGNFILE',
'#HEADUID',
'#PRINCIPALFROM',
'#PRINCIPALTO',
'#EXCEPTIONUSER',
'#LastUpdate')
END
GO
Below is the table structure
[dbo].[APPUSERDTL](
[UID] [int] NOT NULL,
[APPCODE] [varchar](50) NULL,
[APPEXPIRED] [datetime] NULL,
[SIGNIMAGE] [image] NULL,
[SIGNFILE] [varbinary](max) NULL,
[HEADUID] [int] NULL,
[PRINCIPALFROM] [money] NOT NULL,
[PRINCIPALTO] [money] NOT NULL,
[EXCEPTIONUSER] [bit] NOT NULL,
[LastUpdate] [datetime] NOT NULL )
It seems you are passing a varchar value to #SIGNFILE parameter.
Please check the value you are passing
eg. You can replicate this issue
declare #test varbinary(max)=''
select #test
Update
Why you are passing with '' to insert statement? It will consider all the values as varchar if you use within quotes. Remove quotes and try
You can do as below
INSERT INTO APPUSERDTL
(UID,
APPCODE,
APPEXPIRED,
SIGNIMAGE,
SIGNFILE,
HEADUID,
PRINCIPALFROM,
PRINCIPALTO,
EXCEPTIONUSER,
LastUpdate)
VALUES(#UID,
#APPCODE,
#APPEXPIRED,
#SIGNIMAGE,
#SIGNFILE,
#HEADUID,
#PRINCIPALFROM,
#PRINCIPALTO,
#EXCEPTIONUSER,
#LastUpdate)
When the SQL Server trying to perform conversion from the empty string ('') to the varbinary(max) the error occurred. The only way to do is to avoid implicit conversion. If you need to convert the empty string to varbinary type, use the cast(#your_variable as varbinary(max)) construction (in the above code where you calling to you SP SP_FILEUPLOAD from).
Related
I'm trying to bulk insert a table with the code below:
DROP TABLE #temp_FeirasLivres
CREATE TABLE #temp_FeirasLivres
(
ID INT null,
LONG BIGINT null,
LAT BIGINT null,
SETCENS BIGINT null,
AREAP BIGINT null,
CODDIST INT null,
DISTRITO NVARCHAR(100) null,
CODSUBPREF INT null,
SUBPREFE NVARCHAR(200) null,
REGIAO5 NVARCHAR(200) null,
REGIAO8 NVARCHAR(200) null,
NOME_FEIRA NVARCHAR(200) null,
REGISTRO NVARCHAR(50) null,
LOGRADOURO NVARCHAR(100) null,
NUMERO NVARCHAR(200) null default('S/N'),
BAIRRO NVARCHAR(50) null default(''),
REFERENCIA NVARCHAR(100) null
)
BULK INSERT #temp_FeirasLivres
FROM 'DEINFO_AB_FEIRASLIVRES_2014.csv'
WITH
(
FORMAT = 'CSV',
FirstRow = 1
);
The content of file has 880 rows, but I'll show here enough to validate what I'm saying:
879,-46610849,-23609187,355030827000078,3550308005044,27,CURSINO,13,IPIRANGA,Sul,Sul 1,CERRACAO,4025-8,RUA LINO GUEDES,109.000000,MOINHO VELHO,ALTURA DA VERGUEIRO 7450
880,-46450426,-23602582,355030833000022,3550308005274,32,IGUATEMI,30,SAO MATEUS,Leste,Leste 2,JD.BOA ESPERANCA,5171-3,RUA IGUPIARA,S/N,JD BOA ESPERANCA
The error is about the last row has fewer columns than the other rows (there is no, after the previous value).
If I put a "," after BOA ESPERANCA, it works, but I want to know if there is anything I can do on source to save time from always opening and fixing the CSV file.
PS: The last row has a line breaker after it, and I've tried with rowterminator on bulk options, but can try again.
As #Larnu saids in comments:
SQL Server expects the file to be well formed; that means that it has to have the same amount of columns in every row. if the file is malformed (which is appears to be), you'll need to fix the file first, and then BULK INSERT it.
So, it's the best answer.
I got a question related to an translation from SQL Server to Postgres.
I got an table return statement and I don't know how to translate this to Postgres.
my code:
CREATE FUNCTION [dbo].[GetLastUpdatedFluidTreated](#InjectionPoint_ID UNIQUEIDENTIFIER)
RETURNS #Return TABLE (
[RT_FluidTreated_ID] [uniqueidentifier] NOT NULL,
[CL_FluidTreatedType_ID] [uniqueidentifier] NOT NULL,
[Timestamp] [datetime] NULL,
[Last_Updated_Time] [datetime] NULL,
[NewOrModified] [bit] NULL,
[Origin_ID] [uniqueidentifier] NULL,
[IsNew] [bit] NULL
)
AS
BEGIN
INSERT INTO #Return
Select Top 1
x1.[RT_FluidTreated_ID],
x1.[CL_FluidTreatedType_ID],
x1.[Timestamp],
x1.[Last_Updated_Time],
x1.[NewOrModified],
x1.[Origin_ID],
x1.[IsNew]
from RT_FluidTreated x1
WHERE x1.RE_InjectionPoint_ID = #InjectionPoint_ID
Order by x1.[Last_Updated_Time] desc
RETURN;
END
this code above is on sql server.
Can someone help me? thanks
I think a simple set-returning function is what you are looking for:
create function getlastupdatedfluidtreated(p_injectionpoint_id uuid)
returns table (
rt_fluidtreated_id uuid not null,
cl_fluidtreatedtype_id uuid not null,
timestamp timestamp null,
last_updated_time timestamp null,
newormodified boolean null,
origin_id uuid null,
isnew boolean null)
as
$$
select
x1.rt_fluidtreated_id,
x1.cl_fluidtreatedtype_id,
x1.timestamp,
x1.last_updated_time,
x1.newormodified,
x1.origin_id,
x1.isnew
from rt_fluidtreated x1
where x1.re_injectionpoint_id = p_injectionpoint_id
order by x1.last_updated_time desc
limit 1 --<< replaces TOP 1
$$
language sql;
I adjusted the used data types to the most reasonable types in Postgres. You will need to adjust that to the actual types you have used in your table rt_fluidtreated
I have a user defined table-valued function that has an input type of a table variable, and the return type is another table variable.
However, when I try to execute the function it throws the error "Must declare the scalar variable". It looks like the call to the function does not have knowledge of the table variable #myTable, even though it is declared on the previous line.
I think I'm going crazy... can you spot what I am doing wrong?
-- Create the table type
CREATE TYPE [dbo].[TableType] AS TABLE(
[z] [varchar](50) NULL
)
GO
-- Create the user defined function
USE [databaseName]
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER FUNCTION [dbo].[boxPackingSort3](#TableName TableType READONLY)
RETURNS #ReturnTable TABLE(
[partno] [varchar](50) NULL,
[Q] [int] NULL,
[L] [int] NULL,
[W] [int] NULL,
[H] [int] NULL,
[Id] [int] NULL
)
AS
BEGIN
INSERT INTO #ReturnTable SELECT 'xxx', '1' , '1', '1', '1', '1'
RETURN
END
-- Call the user defined function
DECLARE #myTable TableType
INSERT INTO #myTable(z) VALUES('aaa')
select * from dbo.[boxPackingSort3](#myTable)
Msg 137, Level 16, State 1, Line 5
Must declare the scalar variable "#myTable".
I'm fairly new to SQL Server any input and advice would help greatly.
I have 3 tables which are in one-to-many relationships.
Table Person holds customer info
CREATE TABLE [dbo].[Person](
[PID] [int] IDENTITY(1,1) NOT NULL,
[FirstName] [varchar](255) NULL,
[LastName] [varchar](255) NULL,
[CAddress] [varchar](255) NULL,
[Ccity] [varchar](255) NULL,
[Cstate] [varchar](2) NULL,
[Czipcode] [varchar](20) NULL,
[Ccountry] [varchar](255) NULL,
[Cphone] [varchar](25) NULL,
[Cemail] [varchar](255) NULL,
[CipAddress] [varchar](255) NULL)
Table Transaction holds their transaction
CREATE TABLE [dbo].[Transaction](
[TID] [int] IDENTITY(1,1) NOT NULL,
[PID] [int] NOT NULL,
[DateOfTransaction] [date] NULL)
with a third table, TransactionDetail, which holds transaction details
CREATE TABLE [dbo].[TransactionDetail](
[TDID] [int] IDENTITY(1,1) NOT NULL,
[TID] [int] NULL,
[ProductID] [int] NULL,
[ProductName] [varchar](255) NULL,
[ProductQTY] [int] NULL,
[ProductPrice] [decimal](18, 2) NULL)
I would like to create a stored procedure to insert once into the Person table then insert multiple details into the third table.
this is what i got i'm not sure if this is correct?
CREATE TYPE dbo.TransactionTableType AS TABLE
( TID int, ProductID int, ProductName varchar(255), ProductQTY int, ProductPrice decimal(18,2) )
go
CREATE PROCEDURE insertTransacion
#NewProduct dbo.TransactionTableType READONLY,
#FirstName varchar(255),
#LastName varchar(255),
#CAddress varchar(255),
#Ccity varchar(255),
#Cstate varchar(2),
#Czipcode varchar(20),
#Ccountry varchar(255),
#CPhone varchar(25),
#Cemail varchar(255),
#CipAddress varchar(255),
#DateOfTrans date
as
begin
SET NOCOUNT ON;
DECLARE #Pid int
insert into Person(FirstName,LastName,CAddress,Ccity,Cstate,Czipcode,Ccountry,Cphone,Cemail,CipAddress) values (#FirstName,#LastName,#CAddress,#Ccity,#Cstate,#Czipcode,#Ccountry,#CPhone,#Cemail,#CipAddress)
SET #Pid = SCOPE_IDENTITY()
insert into PTransactions(PID, DateOfTransaction) values (#Pid, #DateOfTrans)
DECLARE #Tid int
SET #Tid = SCOPE_IDENTITY()
insert into TransactionDetail(TID, ProductID, ProductName, ProductQTY, ProductPrice) Select #Tid, ntd.ProductID, ntd.ProductName, ntd.ProductQTY, ntd.ProductPrice from #NewProduct as ntd
end
Not sure how to do this in a stored procedure I know how to do it programmatically in asp.net using ado, however I'm trying to avoid that. Sorry for the grammar.
Short answer is you can't, although there are a couple of options open to you.
You can either create a SP to enter the person data, and a separate one to insert the data - one line at a time. Return the person id value with the first call and use that in the subsequent SP calls to insert the data. If you go down this path, make sure you wrap the calling code up in transaction objects so you can roll back the whole lot if you have a problem. You don't state what language you're using for the rest of your code?
Second option is to look at the SQL Bulk Insert command - this is best if you have a lot of data to add into the third table. But involves writing that data out to a file first - slight pain but it's then very fast. Very good if you have thousands or more rows to add.
Couple of other options out there as well depending on your development language.
Cheers
Simon
I've read similar articles here in Stackoverflow, but i can't seem to make it work in my case.
I have column named (Date), with data type varchar, and the data throughout the whole column looks like this (1999-12-31-23-00-01) without parenthesis.
So, instead of Varchar, I want the same value, but in datetime format.
This is my table structure:
CREATE TABLE App
(
[ID] [int] IDENTITY(1,1) NOT NULL,
[Date] Varchar(50) NULL,
[Inst.nr] [smallint] NULL,
[Creator Initials] varchar NULL,
[AppOwner] varchar NULL,
[Status] varchar NULL,
[Serial.Nr] varchar NULL,
[MAC.Addr] varchar NULL,
[Dell.nr] varchar NULL,
[Model] varchar NULL,
[Description] varchar NULL,
[Service Warranty] [date] NULL,
[Purchased] [date] NULL,
)
Actually, the problem is not with the structure, its some old data from an Excel file, which was imported into SQL database.
In my [Date] column, which is in Varchar right now, The data inside this column looks like this 1999-12-31-23-00-01
As you can see, it looks like a datetime format, but it won't accept the conversion from varchar to datetime
Not sure if you are asking the conversion to datetime
SELECT
-- Convert using ISO date time yyyy-MM-dd hh:mm:ss
CONVERT(datetime,
LEFT(Date, 10) + ' ' +
REPLACE(RIGHT(Date, 8), '-', ':')
) AS DateInDatetime
FROM
(VALUES
-- assume format in yyyy-MM-dd-hh-mm-ss
('1999-12-31-23-00-01')
) t (Date)
SQL Fiddle
Use CONVERT function in Sql Server, convert to datetime follow format type 120:
SELECT CONVERT(DATETIME,SUBSTRING('(1999-12-31-23-00-01)',2,10) + ' ' + REPLACE(SUBSTRING('(1999-12-31-23-00-01)',13,8),'-',':'),120)
Hope this help!