Activity displayed as one character only - sql

Good Morning
I created a little procedure where I add activities to a database table.
This is the code I used.
USE dbActivities
GO
CREATE PROCEDURE addActivity
#ActivityDate VARCHAR,
#description VARCHAR,
#maxPeople INT,
#Cost MONEY
AS
BEGIN
INSERT INTO Activity(ActivityDate,Description, MaxPeople, Cost)
VALUES(#ActivityDate, #description, #maxPeople, #Cost)
END
GO
I then select the table to view it.
USE dbActivities
GO
CREATE PROCEDURE viewActivities
AS
SELECT * FROM Activity
GO
The strange thing however is that the Description is displayed as only one character in the datatable. So, if I added the description...say "Swimming", when I view the table it is only displayed with one character 'S'.
Why is that?
regards
Arian

The VARCHAR equals to VARCHAR(1). Use e.g. VARCHAR(60) instead.

Related

Table Variable in SQL Server Function from Input Columns

I would like to create a function that returns a column based on input from three other columns. As temporary tables are not allowed within functions, is it possible to create a table variable from three input columns?
CREATE FUNCTION dbo.convert_value(
#CustomerID VARCHAR(MAX),
#CustomerValue VARCHAR(MAX),
#CustomerDescription VARCHAR(MAX)
)
RETURNS FLOAT
AS BEGIN
DECLARE #CustomerTable TABLE (
UniquePatientUID VARCHAR(MAX),
ResultValue VARCHAR(MAX),
PracticeDescription VARCHAR(MAX)
);
-- How can I insert #UniquePatientUID, #ResultValue and #PracticeDescription into #CustomerTable
END
The context of this question is that I have a SQL script that uses temporary tables and many UPDATE and ALTER TABLE statements, that I need to convert into a function. That script begins with the three columns mentioned, and adds a fourth column, Converted_Value, which is calculated with several hundred lines of code and manipulating temporary tables. Is there any hope here?
A table variable insert is really not different than a regular insert. Don't use temp tables. You can alter the table as well, or just declare it initially with that fourth column and allow it to be NULL.
INSERT INTO #CustomerTable (UniquePatientUID, ResultValue, PracticeDescription)
VALUES(#CustomerID, #CustomerValue, #CustomerDescription);
Don't forget to return the FLOAT.
Table Variable is a table so, you can just use INSERT INTO ... VALUES....
INSERT INTO #CustomerTable (UniquePatientUID,ResultValue,PracticeDescription )
VALUES
(#UniquePatientUID, #ResultValue , #PracticeDescription)
Unless you need a table variable for some specific reason, why not just work with the variables as a derived table expression? i.e.
;with inputs (UniquePatientUID, ResultValue, PracticeDescription) as
(
select #UniquePatientUID, #ResultValue, #PracticeDescription
)
select *
from inputs
Table variables fall out of scope after the function call, and you can't pass table types in or out of functions either. So really all a table variable does here is serve as a means of place keeping that's more familiar to SQL developers. But they're not free, which is the only reason I'm curious what your use case is.
If you don't need to return them as a set or something similar, you can just interact with the variables directly too.

SQL Server created table runs slow

I'm creating a temp table called #PILOTTERR. There are over 40,000 records that I need to insert into this table. This takes about 10 min to execute. Is there a way I can make this faster?
CREATE TABLE #PILOTTERR
(
Zip TEXT,
Office CHAR(4),
Branch NVARCHAR(33),
District NVARCHAR(37),
Region NVARCHAR(26),
SE_Territory NVARCHAR(42),
ISE_Territory NVARCHAR(43)
);
INSERT INTO #PILOTTERR
VALUES ('00544','NY04','Long Island','New York-Long Island','US Northeast','SE-New York/Long Island-A','ISE-New York/Long Island-D')
INSERT INTO #PILOTTERR
VALUES ('01001','MA01','North Boston','North Boston','US Northeast','SE-North Boston-C','ISE-North Boston-B')
etc....
Try this approach:
CREATE TABLE #PILOTTERR (
Zip NVARCHAR(10),
Office CHAR(4),
Branch NVARCHAR(33),
District NVARCHAR(37),
Region NVARCHAR(26),
SE_Territory NVARCHAR(42),
ISE_Territory NVARCHAR(43)
);
Rather than doing multiple insert into statements have only 1:
INSERT INTO #PILOTTERR
VALUES (N'00544','NY04',N'Long Island',N'New York-Long Island',N'US Northeast',N'SE-New York/Long Island-A',N'ISE-New York/Long Island-D'),
(N'01001','MA01',N'North Boston',N'North Boston',N'US Northeast',N'SE-North Boston-C',N'ISE-North Boston-B'),
(etc..)
And if your datatype is NVARCHAR then begin the quotes with N'' thsi way the server does not have to do the conversion from VARCHAR to NVARCHAR.
And replace TEXT with a much smaller sensible datatype such as NVARCHAR(10) which I think should suffice for a ZIPCODE
Better yet as I mentioned in my comment if you are sourcing your data from a excel or csv you can use SQL Server's Import data Wizard. You can find out more about it HERE

SSIS passing a variable to execute sql task (create table parameter)

I have a package that contains two execute sql tasks followed by numerous DFTs. The first has the statement:
select CAST(floor(rand()*1000)+1 AS INT) AS SeqVar
And has the ResultSet Single row - this works perfect. It gives me a random number between 1 and 1000 and passes that value on to a variable I have called SeqVar. (I have also verified that this works)
The problem I am having is in my second execute SQL task where I try and use the SeqVar variable outputted from the first Execute SQL teask as a parameter in the following statement:
IF OBJECT_ID('tempdb.dbo.[##temp1]') IS NOT NULL
DROP TABLE [##temp1]
CREATE TABLE [##temp1] (
[RecID] int IDENTITY(?,1),
[Name] VARCHAR(30),
[ABA] VARCHAR(10),
[Account] VARCHAR(20),
[Type] VARCHAR(1),
[Date] date,
[Amount] money
);
Under parameter mapping I have the SeqVar variable name, Direction is Input, Data Type numeric, Parameter name is 0, and Parameter size is 1000.
The value I get has to go where I have the "?" in the create tempdb statement. I am trying to have my code start at a random number and increment by 1.
I know this would probably be easier with a Script task but that tool is broken on my maching (weird dts pipeline errors). Thanks in advance and this is all in SSIS 2008.
Using identity in this way seems like a strange solution just to start a sequence at some random value. Parameters formatted as ? definitely don't work in that context in SQL.
However, another way to manage this, if the method is a given, is to set the entire code of your SQL task using an expression where you sub in the value as a simple string concatenation and then run the resulting string.
Better do create table as like below then use RESEED property to set it to your Random Variable Value'
IF OBJECT_ID('tempdb.dbo.[##temp1]') IS NOT NULL
DROP TABLE [##temp1]
CREATE TABLE [##temp1] (
[RecID] int IDENTITY(1,1),
[Name] VARCHAR(30),
[ABA] VARCHAR(10),
[Account] VARCHAR(20),
[Type] VARCHAR(1),
[Date] date,
[Amount] money
);
DECLARE #RANDOM_VALUE INT = 50
DBCC CHECKIDENT('##TEMP1',RESEED,#RANDOM_VALUE)

How to compare smalldatetime in stored procedure

I'm writing stored procedure to compare dates but it's not working properly. How can I make it so it compares only the dates but not the time? What I'm trying to do is compare the times and if the Id is null than insert a new entry with the same name but new time. I'm keeping multiple entries with same name but different test time.
ALTER PROCEDURE [dbo].[UL_TestData]
(
#Name varchar(30),
#Test_Time smalldatetime,
#ID INT output
)
AS
Declare #UpdateTime smalldatetime
SELECT #ID=ID FROM Info_User WHERE Name=#Name AND UpdateTime= #Test_Time
IF(#ID IS NULL)
BEGIN
INSERT INTO Info_User (Name, UpdateTime) VALUES (#Name, #UpdateTime)
END
there are a lot of solutions to this depending on what type of DBMS, however here is one:
SELECT #ID=ID FROM Info_User WHERE Name=#Name AND floor(cast(#UpdateTime as float))= floor(cast(#Test_Time as float))
this works because smalldatetime's date is stored a whole numbers, where the time is stored as decimals.
I would cast the dates to a plain date which makes this solution independent of implementation details
select #ID=ID
from info_user
where Name = #Name
and cast (UpdateTime as Date) = Cast(#TestTime as Date)
However, I would either add the date part of the UpdateTime as an additional (calculated) column or split the information into a date and a time part. This makes it much easier to query entries by the plain date.
As a rule of thumb: The type of columns (in general: the table layout) greatly depends on the type of query you usually run against your data.
Edit: As attila pointed out, the date datatype only exists in version 2008 and up

bit Data type usage in Stored procedure

I want to capture Radio button values in db so i created table with datatype bit
But the problem when am create stored Procedure as
CREATE PROCEDURE Mt_Vacancy_mainPreference_insert
(
#post_name varchar(20),#preference varchar(20),gender bit,No_of_vac int
)
AS
Begin
insert into Mt_Vacancy_mainPreference values(#post_name, #preference, #gender, #No_of_vac)
end
RETURN
It says Incorrect Sytax near gender
Must produce scal value
What is the problem
You are missing the # prefix for your last 2 parameter names.
You forgot to decorate the input variables;
gender bit,No_of_vac int
Should be
#gender bit, #No_of_vac int