What is the Problem This code sql server?
syntax error :Msg 102, Level 15, State 1, Line 6 Incorrect syntax near '('.
BEGIN TRAN
exec trn_siparis_insert 'Database',348
exec trn_boyutlu_siparis_olustur , #siparis_id=Select id FROM TABLE.dbo.siparis where kayit_tarihi=(SELECT MAX(kayit_tarihi) FROM TABLE.dbo.siparis ) , #TargetDb=Database
COMMIT TRAN
I don't know if this is the specific error, but this is wrong:
exec trn_boyutlu_siparis_olustur , #siparis_id=Select id FROM TABLE.dbo.siparis where kayit_tarihi=(SELECT MAX(kayit_tarihi) FROM TABLE.dbo.siparis ) , #TargetDb=Database
Perhaps your intention is:
declare #siparis_id int;
Select #siparis_id = id
from TABLE.dbo.siparis
where kayit_tarihi = (SELECT MAX(kayit_tarihi) FROM TABLE.dbo.siparis );
exec trn_boyutlu_siparis_olustur #siparis_id=#siparis_id, #TargetDb='Database'
I suspect that everything that you are doing is poorly thought out. If you want to capture ids being returned by an insert, then an output clause is the right thing to do. Perhaps you should ask another question with sample data, desired results, and an explanation of what you want to accomplish.
Related
Working with SQL Server 2005
Creating Trigger which checks if inserting not already exist.
Having problem getting record parameter, there is the code:
CREATE TRIGGER t_MFShiftTypeOperation ON [CAST$MFShiftTypeOperation]
FOR INSERT, UPDATE AS
IF ##ROWCOUNT=1
BEGIN
IF EXISTS (SELECT * FROM inserted AS I
JOIN CAST$MFShiftTypeOperation AS STO
ON ((I.ShiftTypeCode = STO.ShiftTypeCode AND
I.PackCode = STO.PackCode) OR
I.RegisterAppCode = STO.RegisterAppCode))
BEGIN
DECLARE #Bad_PackCode AS EmpUserCode_t
SET #Bad_PackCode = SELECT TOP(1) PackCode FROM inserted --there is error
ROLLBACK TRAN
PRINT 'Оperation with '+ #Bad_PackCode +' already exist'
END
END
when I'm trying to execute code, it throws me error message:
Msg 156, Level 15, State 1, Procedure t_MFShiftTypeOperation, Line 16
Incorrect syntax near the keyword 'SELECT'.
Can someone explain where is mistake, or suggest better solution.
Change this line:
SET #Bad_PackCode = SELECT TOP(1) PackCode FROM inserted --there is error
to this:
SELECT TOP(1) #Bad_PackCode=PackCode FROM inserted
I'm using SQL Server 2008 R2 (T-SQL) and have dynamic SQL code, which inserting into temporary table some data.
If I run code in new query window or PRINT code and then run in new query window everything is allright, BUT if I try to run this from stored procedure it throw error
Msg 156, Level 15, State 1, Procedure users, Line 3
Incorrect syntax near the keyword 'FROM'.
If I remove line with INSERT INTO #viewsTmp (webId, listId, viewName, viewTitle), code runs fine.
I have similar code above within same stored procedure and that code is fine.
Temp table:
CREATE TABLE #viewsTmp
(
webId uniqueidentifier,
listId uniqueidentifier,
viewName NVARCHAR(128),
viewTitle NVARCHAR(255)
)
variable #databaseName is declared as parameter of stored procedure
#databaseName NVARCHAR(255)
Code which is OK in new query window, but not ok if is runned from stored procedure:
SET #sql = N'
INSERT INTO #viewsTmp (webId, listId, viewName, viewTitle)
SELECT c.tp_WebId, c.tp_ID, b.LeafName, c.tp_Title
FROM (
SELECT *
FROM (
SELECT [ListId], [LeafName], [Type], [WebId]
,ROW_NUMBER() OVER (PARTITION BY ListId ORDER BY DirName) AS RowNumber
FROM ['+#databaseName+'].[dbo].[Docs]
WHERE [LeafName] = ''users''
) AS a
WHERE [RowNumber] = 1 AND [Type] = 1
) AS b
INNER JOIN
(SELECT [tp_WebId], [tp_ID], [tp_Title] FROM ['+#databaseName+'].[dbo].[Lists])
AS c
ON b.ListId = c.tp_ID AND b.WebId = c.tp_WebId
';
--PRINT #sql
exec sp_executesql #sql;
What is wrong with this code?
I decomposed and composed code step by step with small modification as Dale Burrell advised....and code runs and don't runs. I didn't know what was that...and finally I discover one my mistake...
Code itself is ok, but I give #webId as parameter to call inner procedure which is called after code, and there was error. I had all rows with one webId, but exactly one row with another (exception in design) and I pass for this one row same webId as for another rows, and then in this subroutine error appeared. So error message was not for code above, but for some code in called procedure...
Oh my...5 hours of time....
In every way, thank you very much for trying help!
I learned another thing about errors in T-SQL today.
I am trying to create a simple stored procedure in SQL Server 2005, and am puzzled by a syntax error I am getting.
Both tables have identical structure. tbl_Users_non_61 is empty and ready to receive the data.
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE Make_WEEKLY_SUMMARY
AS
BEGIN
SET NOCOUNT ON;
Select (tbl_Users.UserID
, tbl_Users.RegistrationType
, tbl_Users.Datecompleted)
INTO tbl_Users_non_61
FROM
SELECT tbl_Users.UserID
, tbl_Users.RegistrationType
, tbl_Users.Datecompleted
FROM tbl_Users;
END
GO
Resulting error:
Msg 102, Level 15, State 1, Procedure Make_WEEKLY_SUMMARY, Line 5
Incorrect syntax near ','.**
Since you just want to insert records into one table from another, there is a cleaner syntax:
INSERT INTO tbl_Users_non_61(UserId, RegistrationType, DateCompleted)
SELECT u.UserID, u.RegistrationType, u.Datecompleted
FROM tbl_Users AS u
To answer your question though, you do not need the ( ) around your first select, but you DO need them around your sub select, then you need to alias the sub-query you are referencing with the FROM:
Select t.UserID, t.RegistrationType, t.Datecompleted
INTO tbl_Users_non_61
FROM
(
SELECT u.UserID, u.RegistrationType, u.Datecompleted
FROM tbl_Users AS u
) as t;
Try this on:
SELECT
tbl_Users.UserID
, tbl_Users.RegistrationType
, tbl_Users.Datecompleted
INTO tbl_Users_non_61
FROM tbl_Users;
When you SELECT ... INTO, if the columns come from one table only, there's no need to SELECT those same columns again. Even if you had to take data from multiple columns, you still wouldn't need to reselect them, and perform a join instead.
I'm having a weird error that happens under weird circumstances.
The list of skill names I receive in the cursor curLongSkills is to be inserted into the table tbl_new_skill_overview if and only if they don't already exist. So I loop through the cursor as usual, and check whether it already exists before inserting.
The weird thing is that I receive the error Syntax error converting the varchar value 'Some Random Skill' to a column of data type int. on the line SELECT #iCount = COUNT(ID).
However, this does not happen if I remove the WHERE clause in that statement. So if I comment or remove WHERE Name = #sSkillName, it won't give the error. It's as if it thinks that I'm assigning #sSkillName to #iCount just because I'm using #sSkillName in the WHERE clause of the same query.
Other ways of doing this will suffice provided that I can tell whether or not the skill has already been inserted into tbl_new_skill_overview. I don't necessarily have to do it this way.
I've also tried the following, which gives the same error:
SET #iCount = (
SELECT COUNT(ID) AS Line_Count
FROM tbl_new_skill_overview
WHERE Name = #sSkillName
);
The server is running Microsoft SQL Server 2000 (I know, I know...).
Following is the entire SQL script.
DECLARE #sSkillName VARCHAR(200);
DECLARE #iCount INT;
DECLARE curLongSkills CURSOR FOR (
SELECT DISTINCT Name
FROM tbl_new_skill
WHERE Profile = 'long'
AND Parent_ID IS NULL
)
OPEN curLongSkills;
FETCH curLongSkills INTO #sSkillName;
WHILE ##FETCH_STATUS = 0 BEGIN
SELECT #iCount = COUNT(ID)
FROM tbl_new_skill_overview
WHERE Name = #sSkillName; -- No error if this line removed.
IF #iCount = 0 BEGIN
PRINT #sSkillName;
-- TODO: Insert skill
END;
FETCH curLongSkills INTO #sSkillName;
END;
CLOSE curLongSkills;
DEALLOCATE curLongSkills;
I've never liked cursors - but as a cheeky alternative, you should be able to accomplish what you want without a cursor.
insert into tbl_new_skill_overview
select //columnNames
from tbl_new_skill
WHERE Profile = 'long'
AND Parent_ID IS NULL
and name not in
(select name from tbl_new_skill)
The problem was stupidity.
The Name column in tbl_new_skill_overview was mistakenly put in as an INT, not a VARCHAR.
Thanks to all who responded, particularly bobs for asking me to show the database structure, at which point I realized the mistake.
That's a strange occurrence for sure. I have no idea what's causing it, but to get around it, perhaps you could do something like this:
if not exists (select * from tbl_new_skill_overview where Name = #sSkillName) begin
print #sSkillName;
-- TODO: Insert skill
end
That is assuming you don't use #iCount for anything else later.
I've written a stored procedure as following:
CREATE PROC spSoNguoiThan
#SNT int
AS
begin
IF not exists (select column_name from INFORMATION_SCHEMA.columns where
table_name = 'NhanVien' and column_name = 'SoNguoiThan')
ALTER TABLE NhanVien ADD SoNguoiThan int
else
begin
UPDATE NhanVien
SET NhanVien.SoNguoiThan = (SELECT Count(MaNguoiThan)FROM NguoiThan
WHERE MaNV=NhanVien.MaNV
GROUP BY NhanVien.MaNV)
end
SELECT *
FROM NhanVien
WHERE SoNguoiThan>#SNT
end
GO
Then I get the error :
Server: Msg 207, Level 16, State 1, Procedure spSoNguoiThan, Line 12
Invalid column name 'SoNguoiThan'.
Server: Msg 207, Level 16, State 1, Procedure spSoNguoiThan, Line 15
Invalid column name 'SoNguoiThan'.
Who can help me?
Thanks!
When the stored proc is parsed during CREATE the column does not exist so you get an error.
Running the internal code line by line works because they are separate. The 2nd batch (UPDATE) runs because the column exists.
The only way around this would be to use dynamic SQL for the update and select so it's not parsed until EXECUTE time (not CREATE time like now).
However, this is something I really would not do: DDL and DML in the same bit of code
I ran into this same issue and found that in addition to using dynamic sql I could solve it by cross joining to a temp table that had only one row. That caused the script compiler to not try to resolve the renamed column at compile time. Below is an example of what I did to solve the issue without using dynamic SQL
select '1' as SomeText into #dummytable
update q set q.ValueTXT = convert(varchar(255), q.ValueTXTTMP) from [dbo].[SomeImportantTable] q cross join #dummytable p