Error executing stored procedure with uniqueidentifier input parameter - sql

When I execute the stored procedure that has the first input parameter of type uniqueidentifier which is the primary key column in a table that inserts the values into, it is throwing the below error. I couldn't find whats wrong with this input or stored procedure.
EXEC #return_value = DbInsert
#id = EX642793-385D-604F-BE81-0000CD376836,
#name= N'000001_067_0xed642993385d604fbe810000cd376836.tif',
#image = 0x49492A00080000001300FE00040001000000020000000001040001000000C50900000101040001000000E90D0000020103000
SELECT 'Return Value' = #return_value
My stored procedure:
CREATE PROCEDURE DbInsert
#id UNIQUEIDENTIFIER,
#name VARCHAR(255),
#image IMAGE
AS
BEGIN
SET NOCOUNT ON;
INSERT INTO TbImage (Id, Name, Image )
VALUES (#id, #name, #image)
END
Error:
Msg 102, Level 15, State 1, Line 7
Incorrect syntax near '-'.
Update
I tried to right click the stored procedure and execute, and it didn't ask me for quote and all, but the query I wrote above is what it shown as error. When I try with quote, it throw below error
Msg 8114, Level 16, State 1, Procedure DbInsert, Line 0 [Batch Start Line 2]
Error converting data type varchar to uniqueidentifier. (1 row(s) affected)
Update again
Using valid Uniqueidentifier by changing some numbers myself, however replaced X with some other char...still no luck.
EM662993-385D-604F-BE81-0000CD376838

There are 2 issues with your code:
You need to single quote uniqueidentifiers e.g. 'DE5AA552-0601-453C-AF21-9B285FA4E920'.
A guid/uniqueidentifier must contain only valid hexidecimal characters, and X is not a valid hexidecimal character and is therefore not valid within a uniqueidentifier so 'EX642793-385D-604F-BE81-0000CD376836' is not valid but 'EA642793-385D-604F-BE81-0000CD376836' is. You can use http://guid.us/Test/GUID to verify and generate guids.

Related

Operand type clash when compiling a native stored procedure in SQL Server 2019

Any idea why I can compile this stored procedure with the first Insert but not the second (or both)? The error message is:
Msg 206, Level 16, State 2, Procedure InsertExtPageWithXML, Line 21 [Batch Start Line 11]
Operand type clash: numeric is incompatible with uniqueidentifier
THis is the SQL code:
--======================================================
-- Create Natively Compiled Stored Procedure Template
--======================================================
USE [PortalMO]
GO
-- Drop stored procedure if it already exists
IF OBJECT_ID('InsertExtPageWithXML','P') IS NOT NULL
DROP PROCEDURE [dbo].[InsertExtPageWithXML]
GO
CREATE PROCEDURE [dbo].[InsertExtPageWithXML]
-- Add the parameters for the stored procedure here
-- (not inserted is the auto-generated UI [PK_Id], non-null, primary key..)
(#1_Topic_PK uniqueidentifier = NULL,
#1_Path nvarchar(500) = "fix.me",
#1_Title nvarchar(450) = "fix.me",
#1_URL nvarchar(max) = "fix.me",
#1_Priority tinyint = NULL,
#1_Type nvarchar(50) = NULL,
#2_XMLfragment nvarchar(max) = "fix.me")
-- (all of the types above are accurate to the schemas already in existence)
WITH NATIVE_COMPILATION, SCHEMABINDING
AS BEGIN ATOMIC WITH
(
TRANSACTION ISOLATION LEVEL = SNAPSHOT, LANGUAGE = N'us_english'
)
--Insert statements for the stored procedure here
INSERT INTO [dbo].[ExternalPage] (Topic_PK, Path, Title, URL, Priority, Type, LastUpdated)
VALUES (#1_Topic_PK, #1_Path, #1_Title, #1_URL, #1_Priority, #1_Type, GETDATE());
INSERT INTO [dbo].[XML] (Associated_PK, Type, XMLfragment, LastUpdated)
VALUES (SCOPE_IDENTITY(), N'ExternalPage', #2_XMLfragment, GETDATE());
END
GO

How can I insert text, from a subquery, in a function request

I'm using a function on the Master database (xp_fileexist) to check if a folder is empty or not. If it's empty I want a 0, otherwise a 1.
If I hardcode the folder name ('C:\Import\2016-01-01\Transaction') it works fine. What doesn't work for me, is having the date as a variable, as the date changes from time to time. For the variable, I use this:
'C:\Import\The Netherlands\'+CAST((select workingdate from system..tmpworkingdate) AS VARCHAR(10))+'\Transaction(BP)'
This is the code I've tried:
CREATE TABLE #temp (FileExists int, IsDirectory int, ParentDirExists int)
INSERT INTO #temp
EXEC master..xp_fileexist ('C:\Import\'+CAST((select workingdate from system..tmpworkingdate) AS VARCHAR(10))+'\Transaction(BP)')
IF EXISTS(SELECT IsDirectory FROM #temp WHERE IsDirectory=1)
PRINT 1
ELSE
PRINT 0
DROP TABLE #temp
Error: Msg 102, Level 15, State 1, Line 5 Incorrect syntax near
'C:\Import\The Netherlands\'. Msg 156, Level 15, State 1, Line 5
Incorrect syntax near the keyword 'AS'.
Does anyone have a clue?
EXEC doesn't allow string manipulations (or any expression evaluation). Define the value beforehand:
DECLARE #filename VARCHAR(MAX);
SET #filename = 'C:\Import\'+CAST((select workingdate from system..tmpworkingdate) AS VARCHAR(10))+'\Transaction(BP)';
EXEC master..xp_fileexist (#filename);
That said, you should use CONVERT() or FORMAT() to be sure you get the format you really want. You wouldn't want system changes to totally break this code.
EDIT:
Argg! I didn't realize that EXEC master..xp_fileexist doesn't even allow string variables on the execution line. So, you have to do the whole thing as dynamic SQL:
DECLARE #sql NVARCHAR(MAX) = 'EXEC master..xp_fileexist ''' + #filename + '''';
EXEC(#sql);
(There are examples on the web that do use variables, so maybe this depends on the SQL Server version.)

Operand type clash: NULL is incompatible with SmallIntTable SQL Server

I'm trying to execute a simple stored procedure in SQL Server.
USE [dbname]
GO
DECLARE #return_value int
EXEC #return_value = [dbo].[usp_storprocname]
#ID = NULL,
#SomeID = 123456,
#AddressTypeIDs = NULL
SELECT 'Return Value' = #return_value
GO
What I'm getting is an error:
Msg 206, Level 16, State 2, Procedure usp_storprocname, Line 0 [Batch Start Line 2]
Operand type clash: NULL is incompatible with SmallIntTable
From what I understand, it expects to get some kind of an array with numbers in #AddressTypeIDs parameter. I don't need to filter results by that field. How can I pass a correct value there?
Before you execute the stored proc, Declare a variable of type SmallIntTable.
This will be a table with a single column, as you have learned.
Insert whatever values you wish into that table, and then when you Execute the stored procedure, pass that variable as the value for the #AddressTypeIDs parameter.
e.g. as below
USE [dbname]
GO
DECLARE #return_value INT
DECLARE #AddressTypeIDs dbo.SMALLINTCOL;
INSERT INTO #AddressTypeIDs
VALUES (1);
EXEC #return_value = [dbo].[usp_storprocname]
#ID = NULL,
#SomeID = 123456,
#AddressTypeIDs = #AddressTypeIDs
SELECT 'Return Value' = #return_value

Error in SQL Server 2005 stored procedure

SET ANSI_NULLS ON
SET QUOTED_IDENTIFIER ON
GO
ALTER PROCEDURE [dbo].[Prc_InsertUpdate] (#boxone VARCHAR(200),
#boxtwo VARCHAR(200),
#boxthree VARCHAR(200))
AS
DECLARE #num AS INT
SELECT #num = MAX(NUMBER) + 1
FROM updatepage
INSERT INTO [TestDB].[dbo].[updatepage]
([number],
[box1],
[box2],
[box3])
VALUES (#num,
#boxone,
#boxtwo,
#boxthree)
I'm creating this procedure but got this error
Msg 208, Level 16, State 6, Procedure Prc_InsertUpdate, Line 9
Invalid object name 'dbo.Prc_InsertUpdate'.
You are ALTER-ing a stored procedure that does not exist. Use CREATE procedure [dbo].[Prc_InsertUpdate] instead.
Also why isn't number an identity column? Your current approach is inefficient and not safe under conditions of concurrency?

how to pass tablename as parameter in sql server

hello frnds i need to pass a table name as parameter to stored procedure
CREATE PROCEDURE six #tablename nvarchar
AS
SELECT * FROM + #tablename
Go
exec six Entry_sixsigma_mag
it gives error like
Msg 102, Level 15, State 1, Procedure six, Line 3
Incorrect syntax near '+'.
Msg 208, Level 16, State 1, Procedure six, Line 3
Invalid object name '#sixsigma'.
Try something like
CREATE PROCEDURE six #tablename nvarchar(100)
AS
EXEC('SELECT * FROM ' + #tablename)
Go
exec six Entry_sixsigma_mag
Have a look at EXECUTE (Transact-SQL)
But you should also have a look at
SQL Injection
Dynamic SQL & SQL injection
before using this blindly.