What is wrong in this where condition? - sql-server

ALTER PROCEDURE EditEmployee (
#RefNo integer ,
#EmpId nvarchar ,
#Name ntext ,
#Designation ntext ,
#Qualification ntext ,
#Gender ntext ,
#DOB date ,
#Address text ,
#Email ntext ,
#Phone decimal )
as
begin
UPDATE Emp_Sample
SET RefNo=#RefNo,Name=#Name,Designation=#Designation,
Qualification=#Qualification,Gender=#Gender,
DOB=#DOB,Address=#Address,Email=#Email,
Phone=#Phone
where EmpId=#EmpId
END

You need to include #EmpId nvarchar size .
ALTER PROCEDURE EditEmployee (
#RefNo integer ,
#EmpId nvarchar(30) ,
nvarchar [ ( n | max ) ]
When n is not specified in a data definition or variable declaration statement, the default length is 1. When n is not specified with the CAST function, the default length is 30.
HERE

I think you have Cast Problem
Try like this
WHERE EmpId = CAST(#EmpId AS INT)

Related

What is the sys.sp_MSmakegeneration

We have set up a merge replication in SQL Server. One of our biggest deadlocks is this stored procedure up above that is run by subscribers.
CREATE PROCEDURE sys.sp_MSmakegeneration
#gencheck int = 0,
#commongen bigint = NULL,
#commongenguid uniqueidentifier = NULL,
#commongenvalid int = NULL OUTPUT,
#compatlevel int = 90
AS
SET NOCOUNT ON
DECLARE #gen bigint
, #replnick binary(6)
, #dt datetime
, #art_nick int
, #first_ts int
, #makenewrow bit
, #retcode smallint
, #nickbin varbinary(255)
, #maxgendiff_fornewrow bigint
, #count_of_articles int
, #lock_acquired bit
, #lock_resource nvarchar(255)
, #procfailed bit
, #delete_old_genhistory bit
, #close_old_genhistory bit
, #changecount int
, #dbname nvarchar(258)
, #processing_order int
, #prev_processing_order int
, #prev_art_nick int
, #force_leveling bit
, #gen_change_threshold int
declare #cmd nvarchar(4000)
declare #old_bi_gen bigint
declare #bi_view_objid int
--declare #GENSTATUS_OPEN tinyint
--declare #GENSTATUS_MERGE_INSERTED_OPEN tinyint
--declare #GENSTATUS_LOCAL_CLOSED tinyint
--declare #GENSTATUS_TEMPORARILY_CLOSED tinyint
What does this stored procedure exactly do here?
And why should it be repeatedly called while it exists there?

Convert char to smallint SQL

I have column in SQL Server table called Book_pagenumber declared as smallint. One of page contained the number and letter like 50a when I tried to use insert statement as shown below
insert into [Books] (book_id, book_author, Book_pagenumber)
values (50, 'Test', cast('50a' as smallint))
I get an error message
Conversion failed when converting the varchar value to data type smallint
I also used
INSERT INTO [Books] (book_id, book_author, Book_pagenumber)
VALUES (50, 'Test', CAST(CAST('50a' AS VARCHAR(3)) AS SMALLINT)
I got the data inserted but just number 50 without the letter.
Please I need advice how to convert char to smallint without changing the column data type
One option could be
Example
Declare #YourTable table (Book_pagenumber varchar(50))
Insert Into #YourTable values
('50a'),
('123b'),
('175')
Select *
,AsInt = try_convert(int,left(Book_pagenumber,patindex('%[^0-9]%',Book_pagenumber+'a')-1))
From #YourTable
Returns
Book_pagenumber AsInt
50a 50
123b 123
175 175
You can create a function that will clean your string from characters, other than numeric:
-- Create Function that will keep only numeric values in #String
CREATE FUNCTION dbo.udf_NumericOnly (#String NVARCHAR(64))
RETURNS SMALLINT
AS
BEGIN
DECLARE
#Result VARCHAR(5) = N''
, #Position INT = 1
, #Length INT;
SELECT #Length = LEN(#String);
WHILE (#Position <= #Length)
BEGIN
SELECT #Result += CASE WHEN SUBSTRING(#String, #Position, 1) BETWEEN CHAR(48) AND CHAR(57) THEN SUBSTRING(#String, #Position, 1) ELSE N'' END;
SELECT #Position += 1;
END
RETURN #Result;
END
GO
-- Test the Function
INSERT Books (book_id, book_authoer, Book_pagenumber)
SELECT
50
, 'Test'
, dbo.udf_NumericOnly('a4s22df5');

Merge statement issue with on clause SQL Server 2016

I have the following temp tables
CREATE TABLE #Test2ABCD
( VenueID int NOT NULL
, VenueName nvarchar(max)
, VenueAdd nvarchar(max)
, VenueCity nvarchar(max)
, VenueState nvarchar(max)
, VenueCounty nvarchar(max)
, VenueZip nvarchar(max)
, VenuePhone nvarchar(max)
, VenueFax nvarchar(max)
, VenueContactName nvarchar(max)
, VenueContactEmail nvarchar(max)
, VenueContactPhone nvarchar(max)
, VenueWebsite nvarchar(max)
, VenueLat float
, VenueLong float
, VenueRating nvarchar(max)
, MapURL nvarchar(max)
, XMLResult xml);
and
CREATE TABLE #SprocRes2ABCD
( RowID int identity(1,1) not null
, GPSLatitude float
, GPSLongitude float
, City nvarchar(max)
, State nvarchar(max)
, PostalCode nvarchar(max)
, Address nvarchar(max)
, County nvarchar(max)
, MapURL nvarchar(max)
, XMLResults xml);
When I try to execute the following merge statement
MERGE INTO #Test2ABCD t
USING #SprocRes s ON (t.VenueID=s.RowID)
WHEN MATCHED THEN
UPDATE SET t.VenueCounty = s.County
, t.VenueLat = s.GPSLatitude
, t.VenueLong = s.GPSLongitude;
I get the following error:
Msg 207, Level 16, State 1, Line 18
Invalid column name 'RowID'.
I know this has something to do with the "RowId" Column being an identity column but does anyone know how to fix this?
OK so it seems that my eyes have stopped functioning! It was indeed a typo in the final line. Thank you very much for the code review

Error Msg 213: Column name or number of supplied values does not match table definition

I created a table with a stored procedure and I'm trying to insert data into it, but I'm getting this error
Msg 137, Level 15, State 2, Line 49
Must declare the scalar variable "#EmployeeID")
Please help!
Create Table Humanresource.Employee
(
EmployeeID char(3) constraint pkemployeeid primary key,
FirstName varchar(20) not null,
LastName varchar(20) not null,
Emp_Address varchar(30) not null,
Title char(30) constraint ckTitle check(Title in ('Executive','Senior Executive','Management Trainee','Event Manager','Senior Event Manager')),
Phone varchar(20) check(Phone like '[0-9][0-9]-[0-9][0-9][0-9][0-9]-[0-9][0-9][0-9]-[0-9][0-9][0-9]')
)
Stored procedure:
create procedure Auto_EmpID
#FirstName varchar(20),
#LastName varchar(20),
#Address varchar(30),
#Title varchar(30),
#Phone varchar(20)
AS
Declare #EmployeeID char(3)
if exists (select EmployeeID From HumanResource.Employee Where EmployeeID = 'E101')
BEGIN
Select
#EmployeeID = Max(right(EmployeeID, 2))
from HumanResource.Employee
Select
#EmployeeID = CASE
When #EmployeeID >= 0 AND #Employeeid < 9
Then 'E0' + convert(char(3), #EmployeeID + 1)
When #EmployeeID >= 9 AND #EmployeeID < 99
Then 'E' + convert(char(3), #EmployeeID + 1)
END
END
Else
set #Employeeid = 'E101'
insert into HumanResource.Employee
Values(#EmployeeID, #FirstName, #LastName, #Address, #Title, #Phone)
Return
Exec Auto_EmpID 'Bamidele','Ajose','Lagos','11-1111-111-111'
exec Auto_Empid 'Kunle','Awele','Ikeja','22-3332-655-897'
exec auto_empid 'John','George','Benin','33-5555-7654-443'
select * from humanresource.employee
insert into humanresource.employee(#EmployeeID, #FirstName, #LastName, #Address, #Title, #Phone)
values(#EmployeeID = 'e101', 'Baley', 'Carles', 'Los Angeles', '23-3445-434-344')
These 2 lines have a lot of problems:
insert into humanresource.employee(#EmployeeID,#FirstName,#LastName,#Address,#Title,#Phone)
values(#EmployeeID='e101','Baley','Carles','Los Angeles','23-3445-434-344')
You don't need to prefix the # symbol to column names. That should only be used with variables.
The column list contains 6 columns, but you are only supplying 5 values in the values clause. Please add the 6th value or remove the column where you don't want to insert.
If you want to use the value 'e101', you can directly insert that. If you want to use the variable #employeeID, just pass that. The assignment #employeeID = 'e101' is wrong inside the values clause. You could just do set #employeeID = 'e101 to assign the value before the insert statement.
#employeeID is declared as char(3), so even if you wanted to assign the value 'e101' to it before the insert statement, the value would get truncated to 'e10'. So, you must declare it as char(4) at least.
Another thing is that your stored procedure takes 5 input parameters, but you pass only 4 when calling it. If you want to pass only some parameters instead of all, you need to specify default values for each parameter while creating the procedure, something like this:
create procedure Auto_EmpID
#FirstName varchar(20) = 'somefirstname',
#LastName varchar(20) = 'somelastname',
#Address varchar(30) = 'somecity',
#Title varchar(30) = 'sometitle',
#Phone varchar(20) = '111-111-111'

How to retrieve value from SQL server DB?

The following query works fine:
select ProductNumber,Name,Color,ListPrice
from Production.Product
where ProductNumber = 'AR-5381'
Where as when,
I'm writing my code like following to retrieve value from SQL sever:
declare #ProductNum nvarchar
set #ProductNum = 'AR-5381'
select ProductNumber,Name,Color,ListPrice
from Production.Product
where ProductNumber = #ProductNum
I'm not able to get the values. But ProductNumber('AR-5381') is present in the table.
What I'm doing wrong?
Variable is declared with default length which is 1.
nvarchar without specifying length means nvarchar(1)
so following nvarchar varaible only store first character in the string because its length is one.
declare #ProductNum nvarchar
set #ProductNum = 'AR-5381'
SELECT #ProductNum
Output
A
declare #ProductNum nvarchar(2)
set #ProductNum = 'AR-5381'
SELECT #ProductNum
Output
AR
You should specify some length like.
declare #ProductNum nvarchar(255)
set #ProductNum = 'AR-5381'
select ProductNumber,Name,Color,ListPrice
from Production.Product
where ProductNumber =#ProductNum

Resources