Msg 102, Level 15, State 1 - sql

I was just trying to teach myself how to work with databases in SQL Server, watching my first tutorial. The guy got it all right, but I get this error:
Msg 102, Level 15, State 1, Line 6
Incorrect syntax near 'rollno'.
The code:
create database mytestdb
create table mytesttable
(
rollno int,
firstname varchar(50),
lastname varchar(50)
)
Being a beginner, I don't know what to do.

Try this...
create database mytestdb;
GO
USE mytestdb;
GO
create table mytesttable
(
rollno int,
firstname varchar(50),
lastname varchar(50)
);
GO
When you execute all the statements in one go, without the key word (batch sperator GO) sql server tried to execute all the statements as one piece of work. But there are certainly rules in sql server which tell you what statements can or can't be executed as one piece of work(in one batch).
Hence use the key word GO to tell sql server you want these pieces of work executed separately, mind you GO is not T-SQL it is SQL Server Management Studio (SSMS) term.

I noticed, it happens due to Unicode File's Byte Order Mark (BOM). It can preset as a non-visible character, and invalidates the SQL syntax.
If I copy/paste the SQL, it works. If I load the file, it won't.
Here is how to fix. (an example only).
You can remove those marks by opening the file in notepad and saving as an ANSI Encoding while saving the file.
See the first few characters in a plain text file.

Try this
create database mytestdb;
GO
USE mytestdb
create table mytesttable
(
rollno int,
firstname varchar(50),
lastname varchar(50)
);

Related

msg 8152 level 16 state 2 string or binary data would be truncated

I am getting this error while executing a procedure
Msg 8152, Level 16, State 2, Procedure SP_xxxxx, Line 92 String or
binary data would be truncated.
I have created a temp table which I will load the data from main table once in procedure and I would be using this table not the main table as main table has huge volume of data and many unnecessary columns.
When I run the below code from sql server management studio then there is no error but when I run this code from a procedure then their is the above error message.
Insert into abc_TMP // tmp for procedure with required columns
Select
Item,
Description,
size,
qty,
stock,
Time ,
Measure
from abc // main table has many columns
One way to check this issue, is to see length of each value
Assume you have table like below
create table t
(
col1 varchar(10),
col2 varchar(10)
)
Inserts into the table will fail,if you try to insert more than 10 characters,if you try to insert them in a batch, you will not get offending value.
So you need to check its length like below , prior to insert
;with cte
as
(
select
len(col1) as col1,
len(Col2) as col2
from table
)
select * from cte where col2>10
There has been number of requests raised with Microsoft to enhance error message and they have finally fixed this issue in SQL2019.
Now you can get the exact value causing the issue
References:
https://voiceofthedba.com/2018/09/26/no-more-mysterious-truncation/
I suspect that you are looking at the wrong line of code in the stored proc.
When you open the proc, i.e. ALTER... you have a header on the stored proc that will throw the line number out.
If you run this, replacing proc_name with your procedure name:
sp_helptext proc_name
That will give you the code that the procedure will actually run, with accurate line numbers if you paste it into a new window.
Then you'll see where the actual error is happening.
If you want a simple way to prove this theory, put a bunch of Print 'some sql 1', Print 'some sql 2' lines in around the code you think is causing the error and see what is output when the error is thrown.

Firebird with .net driver - drop table if exists

I'm new to Firebird and I'm testing a few things to check out the differences between Fb and SQlite (and the .net driver).
I am trying to do a drop table if exists followed by the creation of a table. In Sqlite I am able to do this by:
command.CommandText = #"DROP TABLE IF EXISTS Persons; CREATE TABLE Persons (
PersonID int,
LastName text,
FirstName text,
Address text,
City text); ";
command.ExecuteNonQuery();
However in Firebird the same query fails. I've read that this is not possible to use IFs directly in Firebird SQL, so I've tried to use:
command.CommandText = #"
EXECUTE BLOCK AS
BEGIN IF EXISTS
(SELECT RDB$RELATION_NAME FROM RDB$RELATIONS WHERE RDB$RELATION_NAME = 'Persons')
THEN DROP TABLE Persons; END CREATE TABLE Persons (
PersonID int,
LastName varchar(255),
FirstName varchar(255),
Address varchar(255),
City varchar(255)
); ";
command.ExecuteNonQuery();
But it fails also with the following error:
Dynamic SQL Error SQL error code = -104 Token unknown - line 1, column
27
Can you please help me on this? I've tried to find more info on the web that could help me, but did not have any luck.
Firebird's SQL syntax doesn't have a drop table if exists, instead use recreate table. recreate table will try to drop the table if it exists before creating it. The syntax of recreate table is - other than recreate instead of create - the same as create table.
Your attempt to use execute block fails for two reasons:
You cannot execute two statements together as a command. If you want to execute a script of multiple statements, you'll need to execute each statement individually or use the FbScript class which will parse the script and execute the individual statements for you.
Even if you execute these statements individually, it will still fail, because PSQL (the stored procedure language used in execute block) does not allow execution of DDL. You can use execute statement to circumvent this limitation, but it is better not to do that. In that way you could also address the previous point by executing both - using execute statement - within the execute block.
Alternatively you could just drop the table unconditionally and catch (and ignore) the resulting exception.

What's wrong with my EXECUTE command?

I'm trying to make a PROCEDURE that makes it easier to store changes users make in their settings (like a server log, you know right) into a table user_settings_changelog. I finally made the PROCEDURE acceptable so my sql program (HeidiSQL) would store it. Now the problem is this: how to properly EXECUTE it. I tried this multiple times with multiple lines of code, but it seemed nothing worked. Can anyone help me out here?
The PROCEDURE query:
DELIMITER $$
CREATE PROCEDURE setting_adjustment_log (name_setting VARCHAR(45),
changed_from VARCHAR(45), changed_to VARCHAR(45), username
VARCHAR(45))
BEGIN
INSERT INTO user_settings_changelog
VALUES (GETDATE(), name_setting, changed_from, changed_to,
username);
END$$
The table user_settings_changelog has 5 columns: date DATETIME, name_setting VARCHAR(45), changed_from VARCHAR(45), changed_to VARCHAR(45) and username VARCHAR(45).
The EXECUTE query:
EXECUTE setting_adjustment_log ('background','black','white','TheCoderNoob');
The error HeidiSQL gives me:
SQL Error (1064): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '('background','black','white','TheCoderNoob')' at line 1
Maybe useful to mention: I am using a version of USBwebserver from a few years ago.
EDIT: I've been looking at the EXECUTE/CALL query in MySQL Workbench for a while, it seems like the database expects something between the name of the procedure and the given data or something. When I hover over it, it reads:
Syntax error: 'background' (single quoted text) is not a valid input at this position
First, you should always include the column names in an insert statement. (There might be a few exceptions, but if you are learning SQL, then sticks with best practices.)
Second, distinguish the input parameters from possible column names.
Third, your code looks like MySQL, so use NOW() or a similar function:
DELIMITER $$
CREATE PROCEDURE setting_adjustment_log (
in_name_setting VARCHAR(45),
in_changed_from VARCHAR(45),
in_changed_to VARCHAR(45),
in_username VARCHAR(45)
)
BEGIN
INSERT INTO user_settings_changelog (date, name_setting, changed_from, changed_to, username)
VALUES (NOW(), in_name_setting, in_changed_from, in_changed_to,
in_username);
END$$
When you call the stored procedure, use call:
CALL setting_adjustment_log('background', 'black', 'white', 'TheCoderNoob');

Early execution of "sp_rename" causes query to fail

I'm having a strange problem with an MSSQL Query that I'm trying to run in Microsoft SQL Server 2014. It is an update script for my database structure. It should basically rename a Column (from Price to SellingPrice) of a Table after its content was merged to another one.
USE db_meta
GO
DECLARE #BakItemPrices TABLE
(
ItemNum int,
Price int,
CashPrice int
)
-- backup old prices
insert into #BakItemPrices
select ItemNum, Price from dbo.ItemInfo
-- merge into other table
alter table ShopInfo
add column Price int NOT NULL DEFAULT ((0))
update ShopInfo
set ShopInfo.Price = i.Price
from ShopInfo s
inner join #BakItemPrices i
on s.ItemNum = i.ItemNum
GO
-- rename the column
exec sp_rename 'ItemInfo.Price', 'SellingPrice', 'COLUMN' -- The Debugger executes this first
GO
This query always gave me the error
Msg 207, Level 16, State 1, Line 13
Invalid column name 'Price'.
I couldn't understand this error until I debugged the query. I was amazed as I saw that the debugger wont even hit the breakpoint I placed at the backup code and says that "its unreachable because another batch is being executed at the moment".
Looking further down I saw that the debugger instantly starts with the exec sp_rename ... line before it executes the query code that I wrote above. So at the point my backup code is being executed the Column is named SellingPrice and not Price which obviously causes it to fail.
I thought queries get processed from top to bottom? Why is the execute sequence being executed before the code that I wrote above?
Script is sequenced from top to down. But some changes to schema is "visible" after the transaction with script is committed. Split your script into two scripts, it can help.

sql server: create table taking forever!

i am doing this:
CREATE TABLE person
(
num INT NOT NULL ,
firstname VARCHAR(20) NULL ,
lastname VARCHAR(30) NULL
);
sql server is just saying "debbuging" and it's not doing anything
is there a problem with the syntax or what?
You clicked on the 'Debug' button (green triangle) instead of 'Execute' (red exclamation)? SSMS is waiting on you to step through the T-SQL lines.
Check if there are locks on the database that would prevent SQL Server from executing a create statement. Since you're creating a new table, check for a database-level locks.
i will suggest the obvious...try restarting sql server?