I'm getting an error when trying to increment a row in my db.
Here's the sql query.
var sql = "UPDATE USERS SET Submissions = CAST (Submissions AS VARCHAR(10)) + CAST (1 AS VARCHAR(10)) WHERE Username="+userEmail;
Any help would be great!
If you're incrementing a row, you don't cast it to varchar(10). You keep it as an integer:
var sql =
"UPDATE USERS
SET Submissions += 1
WHERE Username="+userEmail;
Related
For example this returns a value from a query, which I will then use as a column name.
#A=Select top 1 productid from productlist order by timestamp desc
then I would like this "productid" A to be used in the other table
Select #A from customerlist
then the result is #A value instead of field value in customerlist.
When I use dynamic query, I can get right result.
Why?
(I know I can use join but because this productlist table is dynamic, so let's assume it is a sub query)
You need "dynamic SQL" because SQL will NOT allow you to use a parameter as a column name or a table name. You can only use parameters for data values such as in a where clause where column1 = #val
set #A = 'çolumn1'
Select #A from customerlist -- this fails because it is not allowed
Dynamic SQL is a "hack" to get around those restrictions as the SQL statement is placed into a string along with any value held by parameters.
set #A = 'çolumn1'
set #SQL = 'Select ' + #A + ' from customerlist;'
execute #SQL -- this works, the SQL statement is valid with no parameters as column names
The string formed as #SQL is a complete sql statement without needing any parameters as column names.
Note: the syntax I used here is incomplete and is based on MS SQL Server, different databases will use a different, but similar, syntax.
could u please correct this sqlserver query :
select * from messages where #DepartID In(MsgTo)
#DepartID is a session variable that contains the Department ID.
MsgTo is a column in messages table that contains list of values , ex. : '12','10','13','25' .. etc
i used this code :
cmd.CommandText = "select * from messages where #DepartID IN(MsgTo)"
cmd.Parameters.AddWithValue("#DepartID ", session("DepartID")) ' = 12 for example
Dim da As New SqlDataAdapter(cmd)
Dim dt As New DataTable
da.Fill(dt)
lbmsg.text = dt.Rows.Count.ToString ' returns 0 rows
sorry for my poor english
I think you're just having some syntax trouble. Have you declared the #DepartID variable in SQL? You need to make a comparison to an existing column in your WHERE clause. Like:
SELECT [ColumnName]
FROM [Table]
WHERE [ColumnName] = Value
If your department ID is a text-type column in SQL, you'll have to use single quotes on your input. You can use single quotes anyways in integers like IDs when you query them with an "IN" statement and it will work anyways. Try this:
SELECT *
FROM [messages]
WHERE [MsgTo] = #DepartID
So if you replace your #DepartID variable out with your value and then execute the statement, it will return all information for each row where your [MsgTo] column equals your #DepartID.
If you are passing multiple #DepartIDs, then you would have to pass a comma-delimited text list to the "IN" clause with your variable like the example below:
SELECT *
FROM [messages]
WHERE [MsgTo] IN ('1','5','3','12','30')
--Example where #DepartID = '1','5','3','12','30'
I'm not sure what language you're using to execute the SQL, but if this doesn't work, try encapsulating your SQL statement within an EXEC() like below:
EXEC(
SELECT *
FROM [messages]
WHERE [MsgTo] = #DepartID
)
If your MsgTo column contains a string list of values and you want to search through it for a single #DepartID, then use this code:
DECLARE #DepartID as INT; SET #DepartID = 12; --Hard coded example
SELECT *
FROM [messages]
WHERE [MsgTo] LIKE ('%,'''+#DepartID+''',%')
trying to run the query
select * from customers, TablesList where TablesList.TableName+'ID' =
10 and tableslist.tableid= 123
where the column name obtained from another table. I get the following error
Msg 245, Level 16, State 1, Line 1 Conversion failed when converting
the nvarchar value 'CustomersID' to data type int.
I know I can do something like Select * from customers where customersID = 10
But trying to create CustomersID column name dynamically from another table. The intent it to have TablesList.TableName+'ID' give me CustomersID string that I can use to equate to 10.
My guess is that the value for Tablelist.TableName is Customer so when you do + 'ID' it results in 'CustomerID'. 'CustomerID' is the VALUE that is returned and not the FIELD NAME that gets compared to 10.
Hence when sqlserver try to convert 'CustomerID' to 10 you get an error message telling you that it's not an integer Value.
As far as I know you cannot get a "field name" from a field value directly trough SQL, for that you'd need to create a stored proc or some kind of programming language to build the query dynamically
TablesList.TableName+'ID' generates the string 'CustomersID'. You get the error because your comparison is actually made like this:
'CustomersID' = 10 -- The comparison NVARCHAR = INT produces the error
What i think you're trying to achieve requires dynamic SQL.
The problem with what you have is that your where clause is checking if the value 'CustomerID' is equal to 10. It isn't (and can't) use that string as a column name in that context. You need to use dynamic sql.
Dynamic SQL is where you build up a string which contains the SQL you ulitmately want to run. So as an example, you could do something like this:
declare #sql varchar(max)
set #sql = 'select * from customers where ' + (select top 1 TableName from TableList where tableId = 123) + 'ID = 10'
EXEC(#sql)
This sets the #sql variable to select * from customers where customerID = 10 then runs that statement.
Use Concat:
select * from customers, TablesList where Concat('TablesList.TableName','ID') =
10 and tableslist.tableid= 123
I'm trying to display a value in the same field by using iif statement. But one is INT, another is Decimal and the last one is percentage. So, I CAST them as varchar in SQL. How do I display the result in SSRS field?
Here is an example:
IF #Choice = '1'
BEGIN
SELECT
UserId, CAST(IntNum AS VarChar(10)) AS Result
FROM Sample
END
ELSE IF #Choice = '2'
BEGIN
SELECT
UserId, CAST(DecNum AS VarChar(10)) AS Result
FROM Sample
END
How do I display Result in a SSRS field?
Instead of embedding the logic in your SQL statement, why not do it in the report since (presumably) this is where #Choice is defined?
SQL:
-- #Choice is not needed in this query.
SELECT UserId, IntNum, DecNum
FROM Sample
Report:
Add a Calculated Field in the field listing of your DataSet (call it Result):
=IIF(Parameters!Choice.Value = 1, Fields!IntNum.Value.ToString, Fields!DecNum.Value.ToString)
Now your report will have 4 fields in the DataSet:
UserId (int)
IntNum (int)
DecNum (dec)
Result (string)
I think it would be best to do your logic in a CASE Statement, which is really one column, so it should work well in SSRS.
SELECT UserID,
CASE
WHEN #Choice = 1 THEN CAST(IntNum AS VARCHAR(10))
WHEN #Choice = 2 THEN CAST(DecNum AS VARCHAR(10))
WHEN #Choice = 3 THEN CAST(PercentageNum AS VARCHAR(10))
END AS Result
FROM [Sample]
I am trying to insert data with the query
UPDATE CONTACTS SET internationalmsisdn = +904562038544 WHERE id = 31328
After executing query, the internationalmsisdn column is shown as 904562038544.
Why do I lost + sign ?
Any idea?
To insert a special character as a string you need to have the column type as varchar and pass the values as ,
UPDATE CONTACTS SET internationalmsisdn = '+904562038544' WHERE id = 31328
Hope this helps !!