Create Custom warning SSMS - tsql

I'd like to create a custom warning for every change in table design (like the warning about tables affected).
I created a trigger on database for ALTER_TABLE but i've no idea how to show an alert for that.
Any idea?

-- New message:
EXEC Sp_Addmessage 500021,
10,
'Custom message';
GO
RAISERROR(500021, 10, 1);
-- Replacement of Message:
EXEC Sp_Addmessage 500021,
10,
'Custom message... ',
#Lang = 'us_english',
#With_log = 'false',
#Replace = 'replace';
GO
RAISERROR(500021, 10, 1);
-- Altering the message:
EXEC Sp_Altermessage 500021,
#Parameter = 'with_log',
#Parameter_value = 'true';
-- Dropping the message:
EXEC Sp_Dropmessage 500021;

Related

Don't understand 'CREATE VIEW' must be the first statement in a query batch

I am trying to create a View for temporary data I only need during the SQL run.
I get the error:
"'CREATE VIEW' must be the first statement in a query batch."
I searched on that and see it was asked about on Stack Overflow already here: 'CREATE VIEW' must be the first statement in a query batch
However, my SQL code looks like it should but I still get the error. First, I create a table called #StatusTable but that table wasn't working like I wanted. So I tried creating a View.
DECLARE #DOC INT;
DECLARE #StatusTable TABLE
(
status VARCHAR(8000)
)
INSERT INTO #StatusTable
Select Status = ( select CONCAT(ls.ItemText, '/', st.ItemText ) as Status from Inmate_Legal_Status ils
Left join jt_list_items ls on ls.ItemID = ils.LegalStatusID
Left join jt_list_items st on st.ItemID = ils.StatusTypeId
where ArrestNo = #doc and ils.RecordDeleted = 0
order by ils.CreatedTime desc FOR XML PATH('')
)
-- table
USE #StatusTable;
GO
create view MyView as
Select Status = ( select CONCAT(ls.ItemText, '/', st.ItemText ) as Status from Inmate_Legal_Status ils
Left join jt_list_items ls on ls.ItemID = ils.LegalStatusID
Left join jt_list_items st on st.ItemID = ils.StatusTypeId
where ArrestNo = #doc and ils.RecordDeleted = 0
GO
I get three errors:
Incorrect syntax near GO.
'CREATE VIEW' must be the first statement in a query batch
Incorrect syntax near #StatusTable

Can't get mysql_query to work, mysql_error displays nothing

Recently converted from mysql to mysqli and things were working fine. Connect, select still work fine but now one query fails. Here is the basic code:
Connect to database ($con) - successful.
mysqli_query to select some data from table1 (fn1, ln1, yr1) - successful.
Table data goes to $fn, $ln, $yr after mysql_fetch_array - successful.
Use the data to form an insert:
$sql = "insert into table2 (fn2, ln2, yr2) values ('$fn', '$ln', '$yr')";
mysql-query($con, $sql) or die ("Insert failed: " . mysqli_error($con));
The query fail with the Insert failed message but no reason from mysql_error.
What have I missed?
I've try it. It work. Insert it's ok!
$link = new mysqli($dbhost, $dbuser, $dbpass, $dbname);
$qry = "insert into reputazione (iduser,star,votante,commento)
values(10,5,\"pippo\",\"commento\")";
mysqli_query($link, $qry);
if (mysqli_affected_rows($link) > 0) {
echo "ok!";
}
Okay, I solved the problem by coding all the mysqli commands in-line and not calling functions and passing the sql statements to them. I had functions for connecting to the DB, selecting from one table, inserting into another table and then deleting from the first table.

How to Perl Placeholder Variable for a DBD Mysql

I recently had some help with dbi and using placeholders in Perl for Mysql queries. However I am having an issue when using multiple statements for the previously declared or prepared variable in the dbi script.
Code:
use strict;
use warnings;
use DBI qw(:sql_types);
my $dbh = DBI->connect("DBI:mysql:...", ...);
## TABLE CREATION
$dbh->do("USE test;")
$dbh->do("CREATE TEMPORARY TABLE day5 (id INT, temp VARCHAR(4), time TIME, sumadd INT(11))");
$dbh->do("CREATE TEMPORARY TABLE humid (temp VARCHAR(4), i24 INT (10))");
$dbh->do("INSERT INTO day5 (id,temp,time) VALUES(1,'30','03:00:00') ");
$dbh->do("INSERT INTO humid (temp,i24) VALUES('30',8321) ");
## FAILING CODE
my $inter1 = 'i24'; # Generated value
my $sth = $dbh->prepare("SET \#sumadd5 = (SELECT ? FROM humid WHERE temp ='30') ");
$sth->bind_param( 1, $inter1 );
$sth->finish();
$dbh->do("UPDATE day5 SET sumadd= (SELECT \#sumadd5) WHERE time= '03:00:00' ");
my $sumadd = $dbh->selectrow_array("SELECT sumadd FROM day5");
print "$sumadd\n";
$dbh->disconnect();
$sumadd is undefined, but I expect 8321.
My question is how can I make it so the #sumadd5 variable can be inserted into the above query [UPDATE day5 SET sumadd=]? I have added some remarks in the syntax in the spirit of clarity.
IF I RUN MANUALLY BY APPLYING THE SYNTAX INTO MYSQL REMOVING PERL SPECIFICS MY TABLE UPDATE. IF I RUN THE SCRIPT NOTHING HAPPENS TO THE TABLE AND NO ERRORS ARE DISPLAYED.
I suspect that the break is with the UPDATE, however I can't confirm the $inter1 is being passed to the placeholder.
Problem #1
You never execute the statement you prepare. Use
my $sth = $dbh->prepare(q{...});
$sth->bind_param(1, $inter1);
$sth->execute(); <---
$sth->finish();
Shortcut #1:
my $sth = $dbh->prepare(q{...});
$sth->execute($inter1);
$sth->finish();
Shortcut #2:
$dbh->do(
q{...},
undef,
$inter1,
);
Problem #2
SELECT ? ...
behaves as if you had done
SELECT "i24" ... // As opposed to: SELECT i24 ...
just like
SELECT time ...
behaves as if you had done
SELECT '03:00:00' ...`
You can't build the SQL statement you are executing. You need to build it before you execute it. Use
$dbh->do(q{
SET #sumadd5 = (
SELECT }.( $dbh->quote_identifier($inter1) ).q{
FROM humid
WHERE temp = '30'
)
});

Error when executing query with variables in sp_send_dbmail

I am trying to send emails individually to a list of recipients. I am receiving the error:
Msg 22050, Level 16, State 1, Line 0
Error formatting query, probably invalid parameters
Msg 14661, Level 16, State 1, Procedure sp_send_dbmail, Line 478
Query execution failed: Msg 4104, Level 16, State 1, Server xxxxxx, Line 1
The multi-part identifier "email#example.com" could not be bound.
Here is a simplified version of my code, asssuming table1 is a valid existing table and name and email are existing columns.
declare #current_mailaddress varchar(50), #query varchar(1000)
set #current_mailaddress = 'email#example.com'
set #query = 'select distinct name, email from table1
where email = ' + #current_email
exec msdb.dbo.sp_send_dbmail
#recipients = #current_email,
#subject = 'test',
#query = #query
So according to the error, the formatting (of presumably the #query) is wrong. I can't figure it out. Any ideas?
You need to put the value of #current_email in quotes:
'SELECT ... WHERE email = ''' + #current_email + ''''
To see why, consider what your query currently looks like without it:
SELECT ... WHERE email = email#example.com
Any time you work with dynamic SQL, it's a good idea to PRINT the variable for debugging if you get a strange error; it's usually the case that the SQL string you've built is not the one you're expecting. I suggested an easy way to manage debugging code in another, unrelated answer.

Create trigger sytax error in sybase

Below is the code i am using to create a trigger(before insert):
ALTER TRIGGER "delete_entry_before_inserting" BEFORE INSERT
ORDER 1 ON "XYZ"."ABC"
REFERENCING NEW AS "inserted"
FOR EACH ROW /* WHEN( search_condition ) */
BEGIN
IF EXISTS (select hostname from ABC WHERE hostname = inserted.hostname) THEN
UPDATE ABC
SET days_count = (days_count + 1)
WHERE hostname = inserted.hostname
ROLLBACK TRANSACTION
END IF
END
But the above thing is giving me error as: syntax error near rollback transaction on line 11
what am i doing wrong here?
You are missing a BEGIN TRANSACTION somewhere in your code.
Check this documentation.